Skip to main content

Posts

Static Import Statement

     Static Import Statement 1.5v new features  for-each loop var-args AutoBoxing and AutoUnBoxing Generic Method co-variant return type. Queue Annotation  enum static import                                                                           Static Import introduce in 1.5v  according to sun uses of static import reduces the length of the code and improves readability but according world wide programming expert (like us uses of static import creates confusion and reduces readability hence if there is no specific requirement then it is not recommended to use static import. usually, we can access static members using the class names but whenever writing static import we can access static members directly without a class name. without sta...

Import Statement

      Import statement /* class Test{       public static void main(String... args){               ArrayList arr=new ArrayList()      } } compile Time Error-:              cannot find symbol : class ArrayList             location : class Test                                       ArrayList arr=new ArrayList();                                                                                ^ */ we can solve this problem by using a fully qualified name          ...

Declaration and Access Modifier

                                                              Declaration and Access Modifier java source file structure A java program can contain any number of classes but at most one class can be declared as public.if there is a public class the name of the program and the name of the public class must match other wise we will get compile time error. /* case 1-:if there is no public class then we can use any name and there are no restrictions.  class A{ } class B{ } class C{ } class D{ } save program name any classname.java case 2-: if class B is public then name of program should be B.java other wise we will get compile time error saying class B is public , should be declared in a file named B.java. class A{ } public class B{ } class C{ } class D{ } case 3-:if class B and C declared as public and...

Break and Continue

                                                        Transfer Statement break; continue; label Break-: The break statement in java is used to terminate from the loop, switch and labeled block immediately. When a break statement is encountered inside a loop, the loop iteration stops there, and control returns from the loop immediately to the first statement after the loop. Basically, break statements are used in situations when we are not sure about the actual number of iterations for the loop, or we want to terminate the loop based on some condition. we can use break statements in the following places. inside switch to stop fallthrough.                example-:                int x=0;            ...

Iterative Statement : for-each loop

                                  for-each loop(enhanced for loop) for-each loop introduction in 1.5v . it is specially designed to retrieve elements of arrays and collections. example -; to print elements of one-dimensional arrya. normal for loop int[] x={10,20,30,40,50}; for(int i=0;i<x.length;i++)    System.out.print(x[i]); for-each loop int[] x={10,20,30,40,50}; for(int x1:x)    System.out.print(x1); example-:to print element of the two-dimensional array. for-each loop int[][] arr={{2,2,4},{1,2,4}}; for(int[] a:arr){    for(int x:a){          System. out.print(x);   } } normal for loop int[][] arr={{2,3,5},{1,2,4}}; for(int i=0;i<arr.length;i++){    for(int j=0;j<arr[i].length;j++){              System. out.print(arr[i][j]);    } } e...

Iterative Statement : for loop

                                         For Loop for loop is the most commonly used in java. if we know several iterations in advance the for loop is the best choice. Syntex-: for(initialization_section;condition_check;increment/decrement){   loop body; } curly braces are optional without curly braces only one statement under for loop, which should not be declarative statements.           example-:       1    for(int i=0;i<20;i++)                   int x=10; //invalid      2   for(int i=0;i<20;i++); //valid     3    for(int i=0;i<20;i++)                  System. out.print("Hello");//print 20 time Hello        ...

Flow control part 2 Iterative Method

                                                         Iterative Method Iterative statement while() do-while() for() for-each()    1.5v                                                                           while() If we don't know the number of iterations in advance then we should go for a while loop. Example-: /* while(rs.next()){ } while(e.hasmoreElements()){ } while(itr.hasNext()){ } */ the argument should be the boolean type if we are trying to provide any other type then we will get compile time error. syntax-:   while(b){     ...