Skip to main content

String In Java

String class available in  java.lang package .

java.lang.String

String in java is defined as , an object that represents sequence of characters.In java , objects of String are immutable which means a constant , and can not be changed once created.

String class provide various important method like : 

toUpperCase() - convert String to upper case.

toLowerCase() - convert String to lower case.

length() - to check length of String.

concat() - to concatenate two String .

split() - to split String.

and many other methods ,each method is used to perform some operation on String value.

Ways To Create String in Java :

check below program where we are creating String type variable using different ways :
public class StringDemo {
	public static void main(String[] args) {
		String websiteUrl="https://www.crtr4u.com";
		String subject=new String("java programming");
		char charArray[] = {'P','Q','R','S','T'};	
		String strFromCharArray= new String(charArray);
		byte[] byteArray= {70,71,72};
		String stringFromByteArray=new String(byteArray);
		
		System.out.println(stringFromByteArray);		
		System.out.println(strFromCharArray);
		System.out.println(subject);	
		System.out.println(websiteUrl);
	}
}

Output :
FGH
PQRST
java programming
https://www.crtr4u.com
Performing different operations on String using inbuilt methods :

package com.suv.string;
public class StringDemo2 {
	public static void main(String[] args) {
		String websiteUrl="https://www.crtr4u.com/2023/01/string-in-java.html";
		String name="Swapnil V";
		String []splittedArray=websiteUrl.split("\\.");
				
		for(String str: splittedArray) {
			System.out.println(str);
		}			
		System.out.println(name.toLowerCase());
		System.out.println(name.toUpperCase());
		System.out.println(name.length());
	}
}

  
Output :

https://www
crtr4u
com/2023/01/string-in-java
html
swapnil v
SWAPNIL V
9

  
The java.lang.String class implements Serializable, Comparable and CharSequence interfaces. Immutable String in Java : immutable means unmodifiable or unchangeable , String object in java is immutable.


Popular Posts

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Today we will see solution for Error In Spring Boot : ***************************  APPLICATION FAILED TO START ***************************  Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  Reason: Failed to determine a suitable driver class. --->My Application.yml : server: port: 8081 spring: datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:xe username: sys as sysdba password: root Oracle Version is 21 C Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0. check my pom.xml here : <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/m...

Stream In Java

stream in java 8 : Stream API is used to process collection of objects. stream() method is present in Collection interface from package java.util . Stream is an interface present in java.util package, filter() and map() methods are present in Stream interface. -->when to use filter()  method ? If we want to filter elements from collection based on some boolean condition then we can use filter() method.  for example  : filtering even or odd numbers from list of numbers. -->When to use map()  method ? If we want to create new object for every object in collection then we should use map() method. for example : if we want to increase every number in list by 2 , here every object is getting modified ,we have to use map() in this type of scenario. In below example , we used both filter() and map() methods on stream , inside filter method we applied some condition and in map method we are transforming our object to different one. package com.crtr4u.www; import java...

Class and Object in Java

In Java we have to always deal with objects, now let's understand what is object , how to define object in Java. What is Object In Java ? Object is an entity with STATE and Behavior. Examples of Objects : Bike ,Mobile, Student,Laptop,Car etc. If we consider Bike As Object , Bike Name is state , Bike Color is state , and Running is behavior of Bike,Starting is behavior of bike and stopping is also behavior of Bike. In Java we say , Object is an Instance of class , it means we can create object using class. What is class ? A class is template or blueprint from which objects are created. A class in java can contain fields(variables),Methods(Behaviors),blocks,constructors etc. How To Create class in Java ? Syntax To Create Class :      class College {           String collegeName="COEP";           public void getAdmission(){           System.out.println("In this method we have to wri...