Java

Characters and Strings in Java

Characters and Strings in Java

In Java, characters, and strings are used to represent text. 

Characters 
A character in Java is a single letter, number, or symbol represented by the char data type. Char values are enclosed in single quotes like this: 
Example: 

char c = 'a'; 

One can use escape sequences in Java to use a new line, tab etc. 
Example: 

char newLine = '\n'; 

Strings 
A string in Java is a sequence of characters represented by the String class and is enclosed in double quotes. 
Example: 

String a1 = "Hello"; 
String a2 = "World"; 
String a3 = a1 + ", " + a2 + "!"; // a3 = "Hello, World!" 
String name = "Jane Doed"; 
String firstName = name.substring(0,4); // firstName = "Jane" 
String lastName = name.substring(5); // lastName = "Do" 
String sentence = "The quick brown fox"; 
sentence = sentence.replace("brown", "red"); // sentence = "The quick red fox" 
​​​

Java also provides several built-in methods that can be used to manipulate strings, such as length, trim, toUpperCase, toLowerCase and etc. 
 

Top course recommendations for you

    Docker Swarm Project
    1 hrs
    Beginner
    819 Learners
    4.55  (22)
    Collections in Java
    3 hrs
    Beginner
    4.5K+ Learners
    4.59  (150)
    PyCharm for Beginners
    1 hrs
    Beginner
    2.7K+ Learners
    4.41  (136)
    Git Tutorial
    2 hrs
    Beginner
    10.2K+ Learners
    4.53  (699)
    Create a IPL theme Landing page with CSS and HTML
    1 hrs
    Beginner
    6.8K+ Learners
    4.3  (297)
    PyTest Basics
    2 hrs
    Beginner
    2.7K+ Learners
    4.41  (132)
    Python MySQL
    1 hrs
    Beginner
    11.5K+ Learners
    4.51  (480)
    Python Automation Project
    2 hrs
    Beginner
    6.7K+ Learners
    4.57  (209)
    Python For Android
    2 hrs
    Beginner
    5.2K+ Learners
    4.5  (109)
    Kivy Projects
    2 hrs
    Beginner
    1.8K+ Learners
    4.48  (52)