Page 320 - CITS - Computer Software Application -TT
P. 320

COMPUTER SOFTWARE APPLICATION - CITS


           JAVA String Operators

           In Java, there aren’t specific “string operators” like you might find in languages like JavaScript or Python. Instead,
           Java primarily relies on methods and concatenation to perform operations on strings. Here are some common
           string operations and techniques in Java:
           For Example:-
           1   Concatenation Operator +:

           •  You can use the + operator to concatenate (join) strings together.
           String firstName = “John”;
           String lastName = “Doe”;
           String fullName = firstName + “ “ + lastName;

           Example
           class Concat_Operator {

               public static void main( String args[] ) {
                   String first = “Hello”;
                   String second = “World”;



                   String third = first + second;
                   System.out.println(third);


                   // yet another way to concatenate strings
                   first += second;
                   System.out.println(first);

               }
           }


           Output

           HelloWorld
           HelloWorld

           2   Concatenation with Other Data Types
           •  You can also use the + operator to concatenate strings with other data types, which automatically converts the
              non-string operands to strings.

           int age = 30;
           String message = “My age is “ + ag

           Example
           public class Demo {
              public static void main(String args[]){
                 String st1 = “Hello”;

                 int age = 500;






                                                           307
                               CITS : IT&ITES - Computer Software Application - Lesson 78 - 84





  78
   315   316   317   318   319   320   321   322   323   324   325