12 Replies Latest reply: Mar 5, 2010 6:45 AM by 843789 RSS

    Question about primitive data types

    843789
      Hello!
      I'm just started learned Java. I’m using “Thinking in Java” 4th edition (TIJ4) for this purpose.
      I try to do the “Chapter 1, Exercise 1: (2) Create a class containing an int and a char that are not initialized, and print their values to verify that Java performs default initialization.”
      public class Ex1 {
          public static void main(String[] args){
              int vInt;
              char vChar;
              System.out.println(vInt);
              System.out.println(vChar);
              
          }
      }
      When compiling the class Ex1 I get the next

      E:\work\design\Java\TIJ\1\src>javac Ex1.java
      Ex1.java:10: variable vInt might not have been initialized
      System.out.println(vInt);
      ^
      Ex1.java:11: variable vChar might not have been initialized
      System.out.println(vChar);
      ^
      2 errors

      If I initializing variables it’s Ok:
              int vInt = 1;
              char vChar = 'e';
      “When a primitive data type is a member of a class, it is guaranteed to get a default value if you do not initialize it” (TIJ4).

      Why I get error when compiling the class Ex1 with the not initialized members of a class with the primitive data type?

      Sorry for my English (is not my mother tongue).
        • 1. Re: Question about primitive data types
          843789
          There is no default initialization in methods. Since your variables are defined in main you need to initialize them.

          If you move the variables outside of main thus making them instance variables you shouldn't need to initialize them.

          But then, because main is static, you'll need an Ex1 object to use them like this,
          public class Ex1 {
                int vInt;
                char vChar;
              public static void main(String[] args){
                  Ex1 ex = new Ex1();
                  System.out.println(ex.vInt);
                  System.out.println(ex.vChar);
                  
              }
          }
          • 2. Re: Question about primitive data types
            843789
            public class Ex1
            {
                    int vInt;
                    char vChar;
            
            
            
                public static void main(String[] args)
            {
                    System.out.println(getVInt());
                    System.out.println(getVChar());
                    
                }
            }
            • 3. Re: Question about primitive data types
              EJP
              I don't know what that is supposed to prove but it proves nothing. It doesn't even compile, and the underlying concept is not legal Java.
              • 4. Re: Question about primitive data types
                843789
                Sorry, I mis-posted.
                public class Ex1
                {
                        int vInt;
                        char vChar;
                 
                    int getVInt() {
                        return vInt;
                    }
                
                    char getVChar() {
                        return vChar;
                    } 
                 
                    public static void main(String[] args)
                    {
                        System.out.println(getVInt());
                        System.out.println(getVChar());       
                    }
                }
                I think that is supposed to show in the execise. Both getters (getVInt() and getVChar()) returns null which was implicitly initialized.
                • 5. Re: Question about primitive data types
                  843789
                  That code will not compile because you are making a static reference (in main) to the non-static methods getVInt() and getVChar()
                  • 6. Re: Question about primitive data types
                    843789
                    jakain2 wrote:
                    That code will not compile because you are making a static reference (in main) to the non-static methods getVInt() and getVChar()
                    You are right. TashaVar gave the right answer.
                    public class Ex1
                    {
                         int vInt;
                         char vChar;
                    
                         int getVInt() {
                              return vInt;
                         }
                    
                         char getVChar() {
                              return vChar;
                         } 
                     
                         public static void main(String[] args)
                         {
                              Ex1 ex = new Ex1();
                              System.out.println("vInt=" + ex.getVInt());
                              System.out.println("vChar='" + ex.getVChar() + "'");       
                         }
                    }
                    Strange is that the right quote of
                    System.out.println("vChar='" + ex.getVChar() + "'");
                    is missing when running this in my Eclipse. So it is not clear which initial value is set in vChar.
                    • 7. Re: Question about primitive data types
                      843789
                      Moving variables into a wider scope simply to avoid the enormous effort of initialising them is a pretty poor idea, to be honest.
                      • 8. Re: Question about primitive data types
                        843789
                        Can you tell me how may look the answer to exercise 1?
                        • 9. Re: Question about primitive data types
                          843789
                          Alexandre_T wrote:
                          Can you tell me how may look the answer to exercise 1?
                          You mean why this line

                          System.out.println(ex.vChar);

                          doesn't print any visible output?

                          That's because the default char is the null char '\0' which isn't a visible char. One way to view it is to cast it to an int and then print the int like

                          System.out.println((int)ex.vChar);
                          • 10. Re: Question about primitive data types
                            843789
                            Alexandre_T wrote:
                            Can you tell me how may look the answer to exercise 1?
                            It's pretty much already been done in this thread, hasn't it? Anyways, my comment was just to ward off the seeming implication that the way to avoid "not initialised" errors is to move the variables to a wider scope. It's not. As far as the exercise you're doing goes, well, you initially wrote a method containing 2 variables, not a class.
                            • 11. Re: Question about primitive data types
                              843789
                              TashaYar wrote:
                              Alexandre_T wrote:
                              Can you tell me how may look the answer to exercise 1?
                              You mean why this line

                              System.out.println(ex.vChar);

                              doesn't print any visible output?

                              That's because the default char is '\0' which isn't a visible char. One way to view it is to cast it to an int and then print the int like

                              System.out.println((int)ex.vChar);
                              quit editing your post and re-bumping the thread!
                              • 12. Re: Question about primitive data types
                                843789
                                georgemc wrote:
                                TashaYar wrote:
                                Alexandre_T wrote:
                                Can you tell me how may look the answer to exercise 1?
                                You mean why this line

                                System.out.println(ex.vChar);

                                doesn't print any visible output?

                                That's because the default char is '\0' which isn't a visible char. One way to view it is to cast it to an int and then print the int like

                                System.out.println((int)ex.vChar);
                                quit editing your post and re-bumping the thread!
                                Thank you for help repeating my post.