Posts

Showing posts from November, 2017

Data types and Literals in Java

Image
Data types Data types category:   Data types summary: Literals A constant value which can be assigned to variable is called "Literal". Integral Literals All possible value of Integral data type(byte, short, int and long) are called Integral Literals. Default Value: By default every Integral literal is int type. When you assign a value in long data byte and that value is more than 2147483647(Maximum value for int) then compiler will give you error. Ex: long a = 3000000000; Error: The literal 3000000000 of type int is out of range  Even though we have used long data type and value is also within range of long but still compiler gives error because every integral literal is int type. We can resolve this error by specifying long type explicitly. We can specify long type by suffixing l or L . Ex: long a = 3000000000L; Number System: Integral Literal can be represented as Decimal, Octal and Hexadecimal number systems. Let's

Identifiers in Java

Image
Any name in Java program is called Identifier. It could be a class name, variable name or method name. Question: Find out list of identifiers in below program- public class App { public static void main(String[] args) { int a = 10; } } Answer:  1. Test 2. main 3. args 4. a Rule: 1. The only allowed characters in Java identifier are:     2. Identifiers can't start with digit. 3.  Java identifiers are case sensitive. Below are 3 different variables- int Number =10; int NumbeR =10; int NUBber =10; 4. There is not length limit for Java identifiers but it is not commanded to take more than 15 length. 5.  Reserved words can't be used as identifier. 6. All predefined Java classes name and interface names can be used as identifier. It is legal but not recommended. We have declared variable names as String and Runnable in below program. It is valid. public class App { public static void main(String[] args) { int String = 10; int Runna