Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Regarding Different String Initilization

  Asked By: Carolina    Date: Mar 20    Category: Java    Views: 494
  

There is a simple difference between the two options,
the first one String str="" will create a reference and initialize the string
literal and the str is ready for use without errors.
the second one String str=null only creates the reference of a string not
initilizing it, so if you need to make use you need to initilize dynamically

Eg
class test {
public static void main(String[] args) {
String str="";
System.out.println("String :"+str); // Will not report exception
}
}
--------------------------------------------------------------------------------\
--

class test {
public static void main(String[] args) {
String str=null;
System.out.println("String :"+str); // Will not report compiler error
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Regarding Different String Initilization Or get search suggestion and latest updates.




Tagged: