1. what is the output of following program:
- package com.instanceofjavaforus;
- public class StringDemo{
- public static void main(String[] args) {
- String str="I love java";
- System.out.println(str.charAt(3));
- }
- }
2. what is the output of following program:
- package com.instanceofjavaforus;
- public class StringDemo{
- public static void main(String[] args) {
- String str="I love java";
- System.out.println(str.length());
- }
- }
3. what is the output of following program:
- package com.instanceofjavaforus;
- public class StringDemo{
- public static void main(String[] args) {
- String str1="abc";
- String str2="abc";
- System.out.println(str1.equals(str2));
- }
- }
4. what is the output of following program:
- package com.instanceofjavaforus;
- public class StringDemo{
- public static void main(String[] args) {
- String str1 = "abc";
- String str2 = "abc";
- String str3= new String("abc");
- System.out.println("str1 == str2 ? "+(str1==str2));
- System.out.println("str1 == str3 ? "+(str1==str3));
- System.out.println("str1 equals str3 ? "+(str1.equals(str3)));
- }
- }
Explain any one.
ReplyDelete4th program.
please...
one is stored in heap and other is stored in class memory.so if we compare it is not true.if we use ëqualstoignorecase" it will be true
Delete== operator check only reference and equals method check content
Deletestr1 == str2 ? "+(str1==str2));
ReplyDeletethis will compare the references of the object of str1 and str2(note 1:when object is created with string literal it will get stored in the constant pool region of the heap segment..
note 2:when object is created with new keyword it will get stored in the Non-constant pool region of the heap segment..
note3: constant pool region doesnot allow duplicates of objects
note 4:non-constant pool region allows duplicates)