Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper class

 
pravin kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Lukeg{
public static void main(String argv[]){
Lukeg lg = new Lukeg();
lg.go(argv);
}
public void go(String[] argv){
Integer wiNumber = new Integer(argv[0]);
int i = wiNumber.intValue();/// here
i = i * 2;
System.out.println(i);

}
}

[b]In above code how come value of i (in bold i) becomes 1 which in results in 2 as final output
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you clarify your question? argv[0] is the first entry after the class name on the command line when you interpret it.
 
pravin kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer wiNumber = new Integer(argv[0]);

what is value of wiNumber here?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wiNumber is not a primitive data type, it's an object reference. Integer is a subclass of Number which defines various methods, intValue(), byteValue(), shortValue(), doubleValue(), etc. So wiNumber itself does not have a value. It is a reference to an object which should have at its core an int.

However, there is no way to know what that int value will be unless you know what is being sent it on the command line. args[0] (or whatever you call the String array parameter in your main method) refers to the first String sent it on the command line when the program is interpreted.
 
pravin kumar
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well probably it may sounds stubborn but i just want to know how come value of the i=1 here in the example


Integer wiNumber = new Integer(argv[0]);
int i = wiNumber.intValue();/// here
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to do something like this.

java Lukeg 1
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The program itself will throw a RuntimeException if you don't include at least one token on the command line.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic