• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Basic-About default intialization

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does it gives normal output?Why it does not complain abt tmp not intialized?

class Demo{
String msg="Type is";
public static void main (String args[]){
Demo d =new Demo();
d.showType(1);
}
public void showType(int n){
String tmp;
if (n<0) tmp="positive" ;else tmp="not positive";
System.out.println(msg+tmp);

}
}
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java String is treatd as an object and not as a primitive data type. Only primitve data types need to be initialized before they can be used inside a method. Hope it is clear to you.
Bhaskar
 
Bhaskar Selvaraju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rajani,
I think i may have jumped the gun a little bit there. I tried the same problem with a variable of type int. It still compiles and runs ok. So i am really stumped now and need help FAST.

class Demo
{
String msg="Type is";
public static void main (String args[]){
Demo d =new Demo();
d.showType(1);
}
public void showType(int n){
int tmp;
if (n<0)
{
tmp=2 ;
}
else
{
tmp=4;
}
System.out.println(tmp);
}
}
Waiting for an explanation,
Bhaskar
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any local variable(i.e., a variable declared inside a method) except for arrays has to be initialized explicitly before it can be used.
In your code the compiler compiles perfectly because even though 'tmp' is not assigned a value at the time of initialization; u r initializing it in the 'if' & 'else' condition
In the above example if u comment the 'else' statement u will get a compile error .
hope this helps
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Only the class members can be used without initialization. Here your variable tmp is initialized before it is used in the println statement(inside the if...else block) comment out the else block and compiler will complain.
Any variable defined within the scope of method has to be initialized before it is used.
regards

Originally posted by Bhaskar Selvaraju:
In java String is treatd as an object and not as a primitive data type. Only primitve data types need to be initialized before they can be used inside a method. Hope it is clear to you.
Bhaskar


 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajani,
Namrata is correct, you can initialize the local variable in the try and catch block.Commenting the else block would give an error.
The reason is the local variable has to be initialized before it is being used in the method.
Hence the code snipplet like :

will also compile without errors even though it doesnot have a else block.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please give a code example.
Thanks.
 
Mini Pilla
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx guys!!!
 
expectation is the root of all heartache - shakespeare. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic