string in java


public class StringDemoExample {
public static void main(String[] args) {
String s1 = “nag”;
String s2 = “nag”;
String s3 = s1;
String s4 = new String(“nag”);
String s5 = new String(“nag”);
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
System.out.println(s1 == s4);
System.out.println(s1.equals(s4));
System.out.println(s4 == s5);
System.out.println(s4.equals(s5));
}
}

What is the output of the about example?

how to sort hashmap in java


import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class StringDemoExample {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put(“9”, “EE”);
map.put(“8”, “DD”);
map.put(“7”, “CC”);
map.put(“6”, “BB”);
map.put(“5”, “AA”);
map.put(“4”, “bb”);
map.put(“3”, “dd”);
map.put(“2”, “aa”);
map.put(“1”, “cc”);
System.out.println(“Unsort HashMap values are …”);
printMap(map);
System.out.println(“Sorted HashMap…”);
Map<string, string=””> map1 = new TreeMap<string, string=””>(map);
printMap(map1);
}
private static void printMap(Map<string, string=””> map) {
for (Map.Entry entry : map.entrySet()) {
System.out.println(“Key : ” + entry.getKey() + ”   Value : “
+ entry.getValue());
}
}
}
OUTPUT:

Unsort HashMap values are …
Key : 3   Value :  dd
Key : 2   Value :  aa
Key : 1   Value :  cc
Key : 7   Value :  CC
Key : 6   Value :  BB
Key : 5   Value :  AA
Key : 4   Value :  bb
Key : 9   Value :  EE
Key : 8   Value :  DD
Sorted HashMap…
Key : 1   Value :  cc
Key : 2   Value :  aa
Key : 3   Value :  dd
Key : 4   Value :  bb
Key : 5   Value :  AA
Key : 6   Value :  BB
Key : 7   Value :  CC
Key : 8   Value :  DD
Key : 9   Value :  EE

reverse string in java


import java.util.Scanner;
public class StringReverse {
public static void main(String[] args) {
String original = “”;
String reverse = “”;
Scanner in = new Scanner(System.in);
System.out.println(“Enter a string to reverse”);
original = in.nextLine();
int length = original.length();
for (int i = length – 1; i >= 0; i–)
reverse = reverse + original.charAt(i);
System.out.println(“Reverse string is: ” + reverse);
}
}

sample programs for java beginners


import java.util.Scanner;
public class Demo{
public static void main(String args[]){
int a, b, c, d;
System.out.println(“Enter range of numbers to print their multiplication table”);
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
for (c = a; c <= b; c++) {
System.out.println(“Multiplication table of “+c);
for (d = 1; d <= 10; d++) {
System.out.println(c+”*”+d+” = “+(c*d));
}
}
}
}

sample programs for java beginners


import java.util.Scanner;
public class MultiplyTableDemo{
public static void main(String args[]){
int n, c;
System.out.println(“Enter an integer “);
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println(“Multiplication table of “+n+” is :”);
for ( c = 1 ; c <= 10 ; c++ )
System.out.println(n+”*”+c+” = “+(n*c));
}
}

test your skill?

give comments