This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

print uninitialized primitive type

 
luling yang
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnaks for you prompt reply. It clears my mind.
 
luling yang
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just realized missed the main method

 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic