• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string initialization

 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class MyField{
String s;
public static void main(String argv[]){
MyField m = new MyField();
m.amethod();
}
void amethod(){
System.out.println(s);
}
}




I was reading above question from link
http://www.jchq.net/certkey/0405certkey.htm

did not understand it clearly.

what is difference between null and empty. instead of String can i keep object there instead.
Any ideas, resources,sample code,links, highly appreciated. thanks in advance.
 
Janeice DelVecchio
Saloon Keeper
Posts: 1809
12
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. a String is an object. There's a difference between a null reference to an object and an object that holds no information.

2. I suggest you go out and buy some books. Head First Java is great... if you're practicing for SCJP or whatever they call it now, Sierra and Bates have some good books out. There's also plenty of books you can borrow from the library or buy secondhand if budget is a concern.

It's okay to ask questions, but folks are volunteers here, and you're flooding the forums with your own questions, thus not sharing the volunteering. You should try to work some things out for yourself, then ask questions once you read a bit -- that would be more polite... and let's face it: the rule here is BeNice.

Search google for null vs empty string in java. Next time you post a question, search the internet first (or the forums.... see SearchFirst) -- don't expect people to do all the work for you.

ShowSomeEffort
See the Beginner's FAQ for a TON of info: http://www.coderanch.com/how-to/java/JavaBeginnersFaq
And more info on how to ask questions on JavaRanch: http://www.coderanch.com/how-to/java/HowToAskQuestionsOnJavaRanch

EDIT - also, UseCodeTags
 
Sze Kong Chan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is difference between null and empty.





instead of String can i keep object there instead


No!



I agree with Janeice. The best way to learn is to get a book, download a good IDE (netbean, etc) and hack up the codes to try. a good IDE will flag up any compilation error immediately. It better to learn how to fish, I believe that the spirit the guru are trying to cultivate here
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I agree with all you said ( ), except
Sze Kong Chan wrote:. . . download a good IDE (netbean, etc) . . .
It is probably better to use a decent text editor (as listed here) and use the command line. It takes time and energy to learn how to use NetBeans which is better used for learning Java™.
 
kumarjit banerjee
Ranch Hand
Posts: 32
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mathew Lee wrote:public class MyField{
String s;
public static void main(String argv[]){
MyField m = new MyField();
m.amethod();
}
void amethod(){
System.out.println(s);
}
}


The output of the program will be null instead of NullPointerException because of the following method in java.lang.String

public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}


If an object(other than instance of String) is null then NullPointerException will be thrown.
 
Mathew Lee
Ranch Hand
Posts: 238
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read Kathy Sierra book and couple of other books when i wrote scjp 4 years back. When i look back again those questions to strengthen my knowledge now, many of those concept are again new to me. That time i practiced on notepad with dcommand line execution and ide also. I was wondering how to remember so many concepts all the time to perform best in the project esp so many new technology. I feel overwhelmed and do not no start and end for this java. please advise
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic