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

  1. canRead ()
  2. createNewfile()
  3. canWrite ()
  4. exist ()
  5. Delete ()
  6. getName ()
  7. getAbsolutePath ()
  8. length ()
  9. list ()
  10. 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

  1. system.out :standard output Stream.
  2. system.in  : standard input stream .
  3. system.err : standard error stream


Comments

Popular Posts