Found 9321 Articles for Object Oriented Programming

String in Switch Case in Java

Govinda Sai
Updated on 30-Jul-2019 22:30:21

144 Views

A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.Example Live Demopublic class Test {    public static void main(String args[]) {       // char grade = args[0].charAt(0);       char grade = 'C';       switch(grade) {          case 'A' :             System.out.println("Excellent!");             break;          case 'B' :          case 'C' :   ... Read More

Java string comparison sample code examples

Ramu Prasad
Updated on 30-Jul-2019 22:30:21

82 Views

We can compare Strings in Java using the compareTo() method and the == operator.comapareTo() method: The compareTo() method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.The == operator: You can compare two strings using == operator. But, it compares references to the given variables, not values.Example Live Demoimport java.lang.*; public class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials", str2 = "point";     ... Read More

String compare by == operator in Java

V Jyothi
Updated on 26-Feb-2020 06:23:02

87 Views

You can compare two strings using == operator. But, it compares references of the given variables, not values.ExampleLive Demopublic class Sample {    public static void main(String args[]){       String str1 = "hello";       String str2 = "hello";       System.out.println(str1 == str2);    } }Outputtrue

What is the append method in Java?

Moumita
Updated on 30-Jul-2019 22:30:21

3K+ Views

The append(char c) method of the java.lang.StringBuffer appends the string representation of the char argument to this sequence. The argument is appended to the contents of this sequence. The length of this sequence increases by 1.Example Live Demoimport java.lang.*; public class StringBufferDemo {    public static void main(String[] args) {       StringBuffer buff = new StringBuffer("tuts ");       System.out.println("buffer = " + buff);             // appends the char argument as string to the string buffer.       buff.append('A');             // print the string buffer after appending ... Read More

String compare by equals() method in Java

Jai Janardhan
Updated on 30-Jul-2019 22:30:21

92 Views

The equals() method of the String class compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.Example Live Demopublic class Test {    public static void main(String args[]) {       Integer x = 5;       Integer y = 10;       Integer z =5;       Short a = 5;       System.out.println(x.equals(y));       System.out.println(x.equals(z));       System.out.println(x.equals(a));    } }Outputfalse true false

How to Initialize and Compare Strings in Java?

George John
Updated on 30-Jul-2019 22:30:21

126 Views

You can compare Strings using compareTo() method or, equals() method or, or == operator. Following example demonstrates how to initialize and compare strings in Java. Example Live Demo public class StringDemo { public static void main(String[] args) { String str1 = "tutorials"; String str2 = "point"; // comparing str1 and str2 int retval = str1.compareTo(str2); // prints the return value of the comparison if (retval < 0) { System.out.println("str1 is greater than str2"); } else if (retval == 0) { System.out.println("str1 is equal to str2"); } else { System.out.println("str1 is less than str2"); } } } Output str1 is less than str2

Append a single character to a string or char array in java?

Paul Richard
Updated on 26-Feb-2020 06:04:55

1K+ Views

The append() method in the StringBuffer class adds the specified String to the contents of the current String buffer. Using this method you can append a single character to a string or char array in java.Examplepublic class Test {    public static void main(String args[]) {       StringBuffer sb = new StringBuffer("Hi hello how are yo");       sb.append("u");       System.out.println(sb);    } }OutputHi hello how are you

How is the java memory pool divided?

Rama Giri
Updated on 18-Jun-2020 07:13:47

332 Views

Java memory pool is divided into 5 parts namely −Method area − The method area stores the class code − code of the variables and methods.Heap−The Java objects are created in this area.Java Stack− While running methods the results are stored in the stack memory.PC registers− These contain the address of the instructions of the methods.Native method stacks− Similar to Java stack, native methods are executed on the Native method stacks.

Java (JVM) Memory Types

Kumar Varma
Updated on 18-Jun-2020 07:14:20

3K+ Views

Java Virtual Machine is a program/software which takes Java bytecode (.class files)and converts the byte code (line by line) into machine understandable code.JVM contains a module known as a class loader. A class loader in JVM loads, links and, initializes a program. It−Loads the class into the memory. Verifies the byte code instructions.Allocates memory for the program.The memory in the JVM is divided into five different parts namely− Method area− The method area stores the class code − code of the variables and methods. Heap − The Java objects are created in this area. Java Stack− While running methods the results are stored in ... Read More

Comparing Strings and Portions of Strings in Java

Paul Richard
Updated on 26-Feb-2020 07:17:17

160 Views

Following is an example which compares Strings and portion of strings in Java? Explain with an example.ExampleLive Demopublic class StringDemo {    public static void main(String[] args) {       String str1 = "tutorials point";       String str2 = str1.substring(10);       int result = str1.compareTo(str2);       // prints the return value of the comparison       if (result < 0) {          System.out.println("str1 is greater than str2");       } else if (result == 0) {          System.out.println("str1 is equal to str2");       } else {          System.out.println("str1 is less than str2");       }    } }Outputstr1 is less than str2

Advertisements