Java - HelloWorld Program

Example : 1 Find out length of a string.

							  
public class StringDemo {
	public static void main(String[] args) {
		String str = "Hello world";
	      int len = str.length();
	      System.out.println( "String Length is : " + len );
	}
}

							  

RESULT

							  
String Length is : 11
							  
							  

Example : 2 Concatenating strings with the + operator

							  
public class StringDemo {
	public static void main(String[] args) {
		String str = "Hello," + " world" + "!";
		System.out.println(str);
	}
}
							  

							  

RESULT

							  
Hello, world!
							  
							  

Example : 3 Formating strings with format() methods.

							  
public class StringDemo {
	public static void main(String[] args) {
	float floatVar=12.35f;
	int intVar=10;
	String stringVar="Hello,world!";
	String fs;
	fs = String.format("The value of the float variable is " +
		                "%f, while the value of the integer " +
		                "variable is %d, and the string " +
		                "is %s", floatVar, intVar, stringVar);
						
		System.out.println(fs);
	}
}
							  
							  

RESULT

							  
The value of the float variable is 12.350000, while the value of the integer variable is 10, and the string is Hello,world!
							  
							  

Example : 4 Comparing two strings with equals() methods.

							  
public class StringDemo {
	public static void main(String[] args) {
		String name1 = "xxx";
		String name2 = "xxx";
		String name3 = "zzz";
       
		if(name1.equals(name2))
		{
			System.out.println("name1,name2 are equal");
		}
		else
		{
			System.out.println("name1,name2 are not equal");
		}
		
		if(name1.equals(name3))
		{
			System.out.println("name1,name3 are equal");
		}
		else
		{
			System.out.println("name1,name3 are not equal");
		}
		
	
	}
}
 
							  

RESULT

							  
name1,name2 are equal
name1,name3 are not equal
							  
							  

Example : 5 Combining two strings.

							  
public class StringDemo {
	public static void main(String[] args) {
		
		String str = "This is line1.";
		str = str.concat("This is line2.");
		System.out.println(str);

	}
}
							  
							  

RESULT

							  
This is line1.This is line2.
							  
							  

Example : 6 Changing lower case to upper case

							  
public class StringDemo {
   public static void main(String[] args) {
		
     String s1="hello string";  
     String s1upper=s1.toUpperCase();  
     System.out.println(s1upper);  

  }
}

							  
							  

RESULT

							  
HELLO STRING
							  
							  

BytecodeTechnosolutions.com | Raju