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 javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to