iT Portal

Java basics – Part 3

What are the Primitive Data Types in java?
Integer Types
o Byte(1 byte)
o Short(2 bytes)
o Int(4 bytes)
o Long(8 bytes)

Floating Types
o Float(4 bytes)
o Double(8 bytes)

Textual Type
o Char(2 bytes)

Logical Type
o Boolean(1 byte)

Comments in Java

• A single line comment in Java start with //
// hello
• A multiple line comment starts with a /* and ends with a */
/* Hai
How are you? */

Variables in Java
• Declaring and using primitive data types in Java is similar to C.
• Unlike C, in java variables can be declared anywhere in the program.
• Local Variables
In java, if a local variable is used without initializing it, the compiler will show an error.
class hello{
public static void main(string args[])
int a;
System.out.println(a); // Error

Typecasting of primitive data types
Whenever a larger type is converted to a smaller type, the typecast operator has to be explicitly(naked) specified.
double d=10;
int i;
int =(int)d;

Methods in Java
• All methods should be written inside a class.
• No default return type. So, specifying return type is mandatory.

Arrays in Java
• In java, all arrays are created dynamically.
• new operator is used for dynamic memory allocation.
x=new int[7];
The reference x can be used for referring to an int array.
• Reference in java are very similar to pointers in java.
• A reference type can be assigned ‘null’ to show that it is not referring to any object.
int[] x=null;

Use of ‘this’ keyword in Java
• Refer to the object that has invoked the method.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: