Monday, July 18, 2011

02. How To Write JAVA Programmes

1. Basic Java Program


Code :

// Hello world program in JAVA

class hello
{
public static void main(String args[]) // this is the main method
{
System.out.println("Hello World \n");
}
}

Preview : 


2. JAVA Translation

Java Translation Process 
Java Source Code -> (Java Compiler) -> Java Byte Code -> (Byte Code Interpreter) -> Machine Language -> output

Java Source Code : .java file
Java Byte Code    : .class file

3. Comments & Conventions

// - Single line comment
/*...*/ - block comment ( multiple lines )
  • Java is case sensitive

Code :
public class secondFile
{
public static void main(String[] args)
{
System.out.print(" Java is a very portable language ");
System.out.println(" and can run in different tpes of computers. ");
System.out.println(" this is next line!!!! ");
}
}

Preview : 





4. Keywords


abstractcontinuefornewswitch
assert***defaultgoto*packagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenum****instanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfp**volatile
const*floatnativesuperwhile


*not used
**added in 1.2
***added in 1.4
****added in 5.0


source for the keywords : http://download.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html



5. Identifiers 

Identifiers are Case senstive.
Names given for variables , classes and methods

VALID : employeeName, Emp_number, $Emp_salary
INVALID : employee name, #emp, class
"employeeName" method is called the camel method.


6. Data Types

Primitive Data Types:
Data TypeDefault Value (for fields)
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
String (or any object)  null
booleanfalse

Refernce Data Types: 

Array
Class
Code :
public class tempConv
{
public static void main(String[] args)
{
float celsius = 100;
float fahrenheit ;
//convert to Fahrenheit equivalent
fahrenheit = 32 + ((9 * celsius) / 5 );
System.out.println("celsius temprature");
System.out.println("  " + celsius);
System.out.println("Equivalent Fahrenheit Temperature ");
System.out.println("  " + fahrenheit);
}
}


Preview : 



No comments:

Post a Comment