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 //
• A multiple line comment starts with a /* and ends with a */
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.
public static void main(string args)
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.
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.
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.
Use of ‘this’ keyword in Java
• Refer to the object that has invoked the method.