datatype | Default value |
int | 0 |
char | ‘\u0000’ |
float /double | 0.0 |
Object reference | Null |
Primitive datatype | Range |
byte | -128 to 127 |
short | -32768 to 32767 |
int | -2147483648 to 2147483647 |
long | - 9223372036854775808 to 9223372036854775807 |
char | 0 to 65535 |
- following are valid declaration of package ,import and class declarations
· package a;
import java.io.*;
class A{}
· import java.io.*;
class A{}
- following are invalid declarations ,
· import …..
package ….
class ….
· class…..
import ….
class….
- the package or import statement at the beginning of the file is applicable to all the classes in the file.
- following are valid main declarations
· public static void main(String [] args){}
· public static void main(String []XXX){}//in the place of XXX any thing can be placed
· static public void main(String [] args){}
· public static final void main(String [] args){}
· public static synchronized void main(String [] args){}
· public static strictfp void main(String [] args){}
- following declarations of main method results in runtime error(not compile time error)
· public void main(String [] args){}//static modifier missing
· static void main(String [] args){}//public modifier missing
· public static int main(String [] args){return 1;}//return type must be void
· private static void main(String [] args){}//access modifier is private
· protected static void main(String [] args){}//access modifier is protected
· public static void main(String args){}//argument to main method is a String and not ‘String[]’ (String array)
· public static void main(){}//argument String[] missing
- Any class which implements a given interface must provide implementation to all the methods in the interface unless the class itself is abstract.
- The Runnable interface has one and only one method whose prototype is
· public void run();
- Thread class implements Runnable interface.
- When an array is created then the contents of the array are indexed from 0 to array.length-1 .
Java keywords
package | import | class | extends | interface |
implements | public | private | protected | final |
abstract | strictfp | super | this | byte |
short | int | long | char | boolean |
float | double | static | transient | volatile |
void | return | native | synchronized | try |
catch | finally | throw | throws | for |
while | do | break | continue | switch |
case | default | if | else | new |
instanceof | const | goto | assert | |
- A variable declared within a method will not get it’s default value and hence it if it is used in any expression then the compiler objects that the variable may not be initialized.
- instance variables get their default value.
- Array elements get default value whether it is declared inside a method or declared outside the method.
- backslash characters are
‘\b’ | ‘\f’ | ‘\n’ | ‘\r’ |
‘\t’ | ‘\\’ | ‘\’’ | ‘\”’ |
‘\0’ | ‘\1’ | ‘\2’ | ‘\3’ |
‘\4’ | ‘\5’ | ‘\6’ | ‘\7’ |
In any String or char if value present next to ‘\’(backslash) is other than those mentioned in the above table then it results in compiler error ,for eg
“\/santosh”//results in error
“/\santosh”//results in error
“//santosh”//no error
\\santosh //no error
- given, \u000a = \n and \u000d = \r we cannot use these(\u000a and \u000d) in any
string or char assignment statements. Hence the following will cause compiler error ,
String s1 = “\u000a”;
String s2 = “\u000d”;
These two are interpreted as line terminators and hence results in compiler error.