File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes string initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string initialization" Watch "string initialization" New topic
Author

string initialization

Mathew Lee
Ranch Hand

Joined: Jun 08, 2009
Posts: 238
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

Joined: Sep 14, 2009
Posts: 1686
    
  12

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


When you do things right, people won't be sure you've done anything at all.
Sze Kong Chan
Greenhorn

Joined: Mar 30, 2011
Posts: 10
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

Joined: Oct 13, 2005
Posts: 39393
    
  28
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

Joined: Mar 27, 2011
Posts: 32

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

Joined: Jun 08, 2009
Posts: 238
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: string initialization