Afaik, in java instance variable and class variable are given default value 
based on it's type. False for boolean , 0 for byte and etc. Local variable 
(variable inside method) are slighty different , you must supply an initial 
value for them, if not, the compiler will gives you a compile-time error if you 
try to access uninitialized local variable.


Regards,

Seeraz
-----Original Message-----
From: Nguyen Duc Khoi <[email protected]>
Sender: [email protected]
Date: Fri, 24 Dec 2010 11:21:00 
To: <[email protected]>
Reply-To: [email protected]
Cc: <[email protected]>
Subject: Re: [java programming] Variable declaration

Hi Michel

As i see in your comment

> If no initial value is specified, Java supplies one (but it is not a good
> practice to base the execution of your code on the default value).
>

So, if no initial value,is it be assigned a default value?
I don't think so because if we implement a sample code

int i;
>
System.out.print(i);
>

as expected, 0 value will be printed but we encounter an error.

On Thu, Dec 23, 2010 at 1:11 PM, Mihai DINCA <[email protected]> wrote:

>  Hi Michel
>
> If no initial value is specified, Java supplies one (but it is not a good
> practice to base the execution of your code on the default value).
>
> So, the code:
>
>     ...
>     int i;
>     i = 32;
>     ...
>
> actually does:
>
>     ...
>     int i = 0;
>     i = 32;
>     ...
>
>
> If you are obsessed by performances, you might wish to write the above code
> like this:
>
>     ...
>     int i = 32;
>     ...
>
>
> But the compiler probably does this optimization anyway.
>
> So, the thing that really matters is the clarity of your program. The code
> must be enough explicit for you and anybody that reads it. What makes the
> code easy to read is a matter of taste and experience (so the need to impose
> standards when a whole team develop the same application).
>
> For e.g., your code :
>
>  1 import javax.swing.JOptionPane;
> 2
> 3 public class GetInputFromKeyboard {
> 4
> 5 public static void main( String[] args ){
> 6 String name = "";
> 7 name=JOptionPane.showInputDialog(“Please enter your name");
> 8 String msg = "Hello " + name + "!";
> 9 JOptionPane.showMessageDialog(null, msg);
> 10 }
>
>  can be written also like this:
>
> 1 import javax.swing.JOptionPane;
> 2
> 3 public class GetInputFromKeyboard {
> 4
> 5 public static void main( String[] args ){
> 6 JOptionPane.showMessageDialog( null,
> 7              "Hello " + JOptionPane.showInputDialog(
> 8                                "Please enter your name");
> 9 }
>
>  but for "didactic" purposes, it was put in the first form.
>
> Hope it helps
> Mihai
>
>
> Le 23/12/2010 00:23, Michel a écrit :
>
> Questions 1
> What is the difference between the following 2 declaration statements?
>
> char option;
> option = 'C';
>
> and
>
> char option='C';
>
>
> Questions 2
> Why sometimes we initialize variables and sometimes we don’t?
> For example in the following program, we initialized the string name,
> but we did not initialize the string msg:
>
> 1 import javax.swing.JOptionPane;
> 2
> 3 public class GetInputFromKeyboard {
> 4
> 5 public static void main( String[] args ){
> 6 String name = "";
> 7 name=JOptionPane.showInputDialog(“Please enter your name");
> 8 String msg = "Hello " + name + "!";
> 9 JOptionPane.showMessageDialog(null, msg);
> 10 }
>
>
>   --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]<javaprogrammingwithpassion%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>



-- 
Thanks, best regard!
======================================
Nguyen Duc Khoi
Portal Team
eXo Platform South East Asia (http://www.exoplatform.com)
T: http://twitter.com/ndkhoiits
F: http://facebook.com/ndkhoiits

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to