Ali

Ali

32 Articles Published

Articles by Ali

Page 4 of 4

What is the difference between character literals and string literals in Java?

Ali
Ali
Updated on 30-Jul-2019 1K+ Views

Character literals represents alphabets (both cases), numbers (0 to 9), special characters (@, ?, & etc.) and escape sequences like , \b etc. Whereas, the String literal represents objects of String class. Example Live Demo public class LiteralsExample { public static void main(String args[]){ char ch = 'H'; String str = "Hello"; System.out.println("Value of character: "+ch); System.out.println("Value of string: "+str); } } Output Value of character: H Value of string: Hello

Read More

Do local variables in Java have default values?

Ali
Ali
Updated on 30-Jul-2019 1K+ Views

No, local variables do not have default values. Once we create a local variable we must initialize it before using it. Since the local variables in Java are stored in stack in JVM there is a chance of getting the previous value as default value. Therefore, In Java default values for local variables are not allowed. Example public class Sample { public static void main(String args[] ){ int data; System.out.println(data); } } Error C:\Sample>javac Sample.java Sample.java:4: error: variable data might not have been initialized System.out.println(data); ^ 1 error

Read More
Showing 31–32 of 32 articles
« Prev 1 2 3 4 Next »
Advertisements