String Literals – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Code Learning #JAVA#3818    siteicon   siteicon  

String Literals

Use of string literals as chain of normal alphabets, unicode characters, octal characters, escape sequences etc.

Learning Objectives

  • String literal and its variations.

Source Code

TC++ #3818

Source Code

Run Output

Hello World!
Hello Man!
Hello Woman!
Hello Mankind!
"Hello Aliens"
Hello GOD

Code Understanding

System.out.println(“Hello World!”);
Here string literal has been used as argument to the println() method.

String s1=”Hello Man!”;
Here string literal has been assigned to the object of string class.

String s2=”Hello Womanu0021″;
The unicode character in u0021 the 16 bit hexadecimal form can be given anywhere in the string literal. Here the code of ! character has been given.

String s3=”Hello Mankind41″;
Here the octal character 41 the three digit form has been given. 41 is octal code for ! character

String s4=””Hello Aliens””;
Here the escape sequence for double quote character which is ” has been given. This allows printing of text within double quotes/

String s5=”HellotGOD”;
Here the escape sequence for tab character which is t has been given within the string literal

System.out.println(s1); System.out.println(s2); System.out.println(s3); 
System.out.println(s4); System.out.println(s5);
This prints the required output on the screen.

Common Errors

  • Sometimes students assign string literals to char arrays like char a[]=”Hello”; This may be okay in C/C++ but it is not permitted in Java. Java needs String class like String s=”Hello”; You do not have the concept of null terminated strings in java.


Suggested Filename(s): StringLiteral.java



Share

sunmitra| Created: 12-Mar-2018 | Updated: 12-Mar-2018|






Back