Java Basics - Lecture 2 - String concatenation in Java



In previous lecture we have covered Java, Eclipse, Data types and println(), print() methods. Now lets move forward on next topics


Lecture 2 Agenda


  1. String Concatenation
  2. If-else conditions

String Concatenation


- Addition of multiple strings is called string concatenation
- “+” sign is called concatenation operator
    
        int a = 100;
        int b = 200;
        String x = "Hello";
        String y = "World";
        System.out.println(a+b);
        System.out.println(x+y);
        System.out.println(a+b+x+y);
        System.out.println(x+y+a+b);
        System.out.println(x+y+(a+b));
        System.out.println(a+x+b+y);

Output:
       300
       HelloWorld
       300HelloWorld
       HellowWorld100200
       HelloWorld300
       100Hello200World


If-Else Conditions


- Are used to compare data
- Comparison operators <, >, <=, >=, ==,!=, &

       int a = 10;
       int b = 20;

       if(b>a) {
       
          System.out.println("b is greatest");
       }
       
       else {

       System.out.println("a is greatest");
     }

b is greater than a, condition is true so if statement will be executed.



      if(b<a) {

      System.out.println("b is greatest");
     
     }

     else {

     System.out.println("a is greatest");
}

- b is greater than a, condition is false so else statement will be executed.



      
       int c = 40;
       int d = 50;
       
       if(c==d{

       System.out.println("c and d are equal");
        
       }

       else {

       System.out.println("c and d are not equal");

   }



- Find a highest number

       int a = 100;
       int b = 200;
       int c = 300;

       if(a>b & a>c) {

       System.out.println("a is greatest");

       }

       else if(b>c) {

       System.out.println("b is greatest");

       }

       else {

       System.out.println("c is greatest");

}


0 comments:

Post a Comment