Basic of Java
Basic of Java
First code line
public class class name
The main Method
public static void main ()
Print text
System.out.println("Hello world")
Data types
int , char , float , double , void , bool
Comments
//single line commnets
/*Multiline commnet /*
Arithmetic Operators
+(Addition) , -(Subtract) , *(Multiplication) , /(Division)
Relational Operators
<=(less than or equal to) , <(less than) , >(greater than) , >=(greater than equal to) , ==(equal to equal to)
Logical operator
||(logical OR) , &&(logical AND) , !(logical NOT)
For loop
for(statement1;statement2;statement3)
While loop
while (condition)
{
//code block
}
If-else
if(condition)
{
//code of true condition
}
else
{
//code of false condition
}
Arrays
// declare array
string[] cars;
//Array of Integers
int[] my num=(10,20,30,40}
Switch
Switch (expression) {
case value 1:
//code to be executed
break;
//continue
default :
//code to be executed if all cases are not matched
}
File Methods
- canRead ()
- createNewfile()
- canWrite ()
- exist ()
- Delete ()
- getName ()
- getAbsolutePath ()
- length ()
- list ()
- mkdir ()
Create a class
//a class with name "main" with variable x.
public class Main {
int x=5 ;
}
Create an object
// object will be create of name "myobj".
public class Main {
int x=5 ;
public static void main (String[]args)
Main myobj = new Main () ;
System.out.println(myobj.x) ;
}
}
I/O Operatos
- system.out :standard output Stream.
- system.in : standard input stream .
- system.err : standard error stream
Comments
Post a Comment