Saturday, May 19, 2007

Primitive Data Types

Java is Strongly typed. meaning is we have to declare the variable before we can use them.

e.g. double speed=30;

Above statement in a code indicates that field named 'speed' exists and holds a numeric value.Fields data type determines what kind of value it will hold (whole number/with fractional part etc.)like 'int' will have only whole number whereas double (in above example) can have fractional part as well.

Apart from int,double there are 6 more data types in Java.Let's go into the details of all one by one.

1) byte
8-bit Signed Integer
Minimum Value = -128 (Inclusive)
Maximum Value = 127 (Inclusive)
Negative numbers are stored in 2's complement form.

2)short
16-bit Signed Integer
Minimum Value = -32,768(Inclusive)
Maximum Value = 32,767 (Inclusive)
Negative numbers are stored in 2's complement form.

We can use a 'byte' and 'short' to save memory in large arrays (if we know that value will not cross maximum & minimum limit), in situations where the memory savings actually matters.

3)int
32-bit Signed Integer
Minimum Value = -2,147,483,648(Inclusive)
Maximum Value = 2,147,483,647(Inclusive)
Negative numbers are stored in 2's complement form.


4)long
64-bit Signed Integer
Minimum Value = -9,223,372,036,854,775,808 (Inclusive)
Maximum Value = -9,223,372,036,854,775,807 (Inclusive)
Negative numbers are stored in 2's complement form.


5)float
single-precision 32-bit IEEE 754 floating point


6)double
single-precision 64-bit IEEE 754 floating point

As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers.



7)boolean
Represents one bit of information.Only two possible values true and false.
Usage of this data type is as a simple flags to track true/false conditions.
It's size is not defined precisely


8)char
single 16-bit Unicode character
Minimum Value = '\u0000' (Inclusive)
maximum Value = '\uffff' (Inclusive)

Java also provides special support for character strings via the 'String' class.
Although String is not a Primitive Data type.
for example,
String s = "this is a simple string";

String objects are immutable, which means that once created, their values cannot be changed.

Default Values of Primitive Data Types

Fields (class level data members) that are declared but not initialized will have some default values. These values will be assigned by the compiler. These default values are either Zero ('0') or null.

For byte,short, int default value is '0'.
For long it id '0L'
For Float it's 0.0f,whereas default value for double is 0.0
char will have '\u0000' as a default value whereas String (or any other object for that matter) has 'null' as its default value.
boolean has 'false' as it's default value.

Note:
1) Local variables are never initialized by the compiler.
2) As a programmer, we have to initialize a local variable where it is declared.
3) If we try to access an uninitialized local variable , we will get a compile time error.

Saturday, May 5, 2007

Java Language Basics - Conventions used for Naming Variables

Every programming language has its own set of rules and conventions for the kinds of names that you're allowed to use.The rules and conventions for naming your variables as far as Java is concerned are given below:

1.Variable names are case-sensitive

2.A variable's name can be an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".
Note: Normally the convention is to start a variable name with letter & not with $ or underscore (_) character.

3.White space in variable name is not permitted.

4.Subsequent characters after first letter may be letters, digits, dollar signs, or underscore characters

5. Use full words instead of cryptic abbreviations; this will make your code easier to read and understand.
e.g. use variable name as 'speed' instead of 's' etc..

6.Variable name can't be standard keywords/reserved words
Visit the following Link to see the list of Java Keywords & Reserved words
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html

7.If variable name is a single word name that in lowercase
e.g. int speed;

8.If a variable name has more than one word, then start with lowercase and then capitalize the first letter of each subsequent word
e.g. int numberOfGears; or double capitalPrice;

9.If your variable stores a constant value (in Java constants are marked with attaching a 'final' keyword before variable name)such as
static final int NUMBER_OF_GEARS = 6;
(NUMBER_OF_GEARS is a constant static class variable whose value can't be changed)

the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.


In the next post we will have a look at "Primitive Data Types" in Java.

Friday, May 4, 2007

Java Language Basics - Variables

In this and subsequent posts we will try to understand very basic concepts in Java Programming Language namely,

Variables
Conventions used for Naming Variables
Primitive Data Types
Arrays
Operators
Expressions,Statements and Blocks
Control Flow Statement

Those who are already aware of C,C++ languages will be knowing most of the stuff, However there could be few things you may learn which are java specific in this post.
So, I would recommend them to read this post as well.


Let's begin

Varaibles

As we have seen earlier, an object stores its state in fields.In the Java programming language, the terms "field" and "variable" are both used; this is a common source of confusion among new developers, since both often seem to refer to the same thing.

The Java programming language defines the following types of variables:

Instance Variables (Non-Static Fields)
Class Variables (Static Fields)
Parameters
Local Variables

Instance Variables (Non-Static Fields)
Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words);
e.g. 'Speed' variable is applicable to each car and this variable will decide the state of an object 'Car'. So In general, objects store their individual states in "non-static fields".

Class Variables (Static Fields)
A class variable is any field declared with the "static" keyword; this tells the compiler that there will be exactly one copy of this variable in existence, regardless of how many instances of this class are creted.
e.g. Number of Gears for specific model of a car will remain same acoross all the models so we could define it as static. i.e. static int numberOfGears =5;

Parameters
We've already seen examples of parameters, Signature for the main method is public static void main(String[] args) Here, the args variable is the parameter to this method.The important thing to remember is that parameters are always classified as "variables" not "fields". This applies to other parameter-accepting constructs as well like constructors etc. which we will see later.

Local Variables
Local variable are declared between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
e.g. int count=10;
There is no special keyword which will distinguish them as local, they are used by the methods in which they are declared to store their temporary state

It's always recommened that, you initialize the local variable where you declare it.


In the next post we will focus on "Conventions used for Naming Variables".

Till then bye bye
 
online degree programs
visit us .