File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes print uninitialized primitive type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "print uninitialized primitive type" Watch "print uninitialized primitive type" New topic
Author

print uninitialized primitive type

luling yang
Greenhorn

Joined: Oct 21, 2006
Posts: 4
I want to print the uninitialized primitive type, here is the code I am using

public class Ex1 {

public static void main(String[] args) {
int x;
System.out.println(x);
}
}



It gives me error when I complied it. However I do remember we don't need to initialize the primitive type before using them. System will give them a defualt value.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No, that is not true for local variables.

Instance variables which are primitive will be given default values if you don't assign them.

But for local variables, you have to assign them a value before you try to use an expression that depends on their value.
luling yang
Greenhorn

Joined: Oct 21, 2006
Posts: 4
Thnaks for you prompt reply. It clears my mind.
luling yang
Greenhorn

Joined: Oct 21, 2006
Posts: 4
Then I modified the code to



However error messages showed up when compiling it. Looks like the compiler doesn't like System.out.println(data.i)
luling yang
Greenhorn

Joined: Oct 21, 2006
Posts: 4
Just realized missed the main method

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by luling yang:
... Looks like the compiler doesn't like System.out.println(data.i)

The compiler doesn't like println statements outside of method bodies (or initializer blocks). Try something like...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by luling yang:
...System.out.println(data.d=1); ...

The goal was to see default initialization, right? Are you getting what you expected here?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print uninitialized primitive type
 
Similar Threads
Final Variable Declaration
Doubt in assigning Objects
Operator
overloading of main()
Determine type of a primitive