Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Beginner in Java, something occured which I can't explain.

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Beginner in Java, something occured which I can't explain.

    Hello, I am very basic to Java. I was programming when something strange occured, I have isolated the problem so that you need to read as little as possible.
    First a short code, where there is nothing wrong, but shows the essence of what I am trying to do.
    class Assign2 {
        public static void main(String[] args) {
    	int val;
    	int[] test = new int[20];
    	if (1>0) {
    	    val = 9;}
    	test[val] = 25;
        }
    }

    I initialize an index called val, but I don't give the index a value uintil the if/else runs. But as you can see the array test[9] gets value 25 the program runs without problems.
    Now look at another code:
    class Assign {
        public static void main(String[] args) {
    	int [] row = new int[10];
    	for (int i = 0; i < 1; i++) {
    	    int index;
    	    //int index = 10000;
    	    for (int j = 0; j < 1; j++) {
    		if (1 > 0){
    		    index = 5;}
    	    }
    	    row[index] = 8;
     
    	}
        }
    }

    Please note that the for-loops only run once, but I still have them there, because the problem occured when I was working with a double for-loop. In the first for loop I initialize the index but I do not give it a value. In the second for loop there is an if test which allways is true, now there I give the index a value, then we go back to the first foor loop, and use the index in the array. Here comes the tricky part: This program gives an error when I try to compile it:
    Assign.java:11: error: variable index might not have been initialized
    row[index] = 8;
    However if I give the index a value 1000(I comment it our in the code I gave you //). The program runs fine. Why does one case work, and the other case does not work?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Beginner in Java, something occured which I can't explain.

    Interesting. In one case the compiler must evaluate the if and remove it which leaves the assignment of val.
    In the other with the assignment inside a loop it doesn't evaluate the loop control.


    When the compiler complains, fix the code and move on.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Beginner in Java, something occured which I can't explain.

    Basically, the Java compiler is not that smart.

    It can identify the expression 1 > 0 as always being true (it's defined as a constant expression) because it contains only primitive literals and the greater than operator.

    However, the expression j < 1 is not constant because j is a variable. The compiler therefore can't determine if the for-loop will always execute, even though we can easily tell that it will.

    Same goes for this code:

    int i = 0;
    int idx;
    int[] data = new int[20];
    if (i < 1)
    {
    	idx = 5;
    }
    data[idx] = 3; // compiler error

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Beginner in Java, something occured which I can't explain.

    Thanks guys, it makes very much sense now.

    Since I have your attention I would like to ask you some general questions about this topic aswell, I hope you can spare the time.
    I have only programmed a little in Python before, so comparing which errors come in java when we compile vs. when we run is very interesting. Can we roughly say that the compiler in java handles syntax-errors and also checks that every variable we use be initialized with a value, here it is very strict as we saw. While run-errors can handle errors like illegal values(out of range etc.?) Or said in another way, if you get a run-error you have made a mathematical mistake, or you may have programmed something which you didn't intend?

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Beginner in Java, something occured which I can't explain.

    Can we roughly say that the compiler in java handles syntax-errors and also checks that every variable we use be initialized with a value
    More or less, though the second part is redundant because accessing an un-assigned local variable is a syntax error in Java.

    A better definition of compiler error is any error which can be caught at compile time. These are violations of the Java Language Specifications, which deal strictly with the grammar and syntax of the Java language.

    For example, JLS Chapter 16 deals with definite assignment. It states:

    Each local variable (14.4) and every blank final field (4.12.4, 8.3.1.2) must have a definitely assigned value when any access of its value occurs.
    Reading through the rest of the chapter you can tell what conditions must be satisfied to ensure that the variable is definitely assigned.

Similar Threads

  1. New to Java, could you explain something to me?
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 6
    Last Post: September 18th, 2011, 07:12 AM
  2. send image file from java to php broken pipe exception occured
    By ashi123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2011, 02:53 PM
  3. Error occured during attachment of file using java.
    By kaliyaodi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 4th, 2011, 05:30 PM
  4. Replies: 1
    Last Post: December 13th, 2010, 04:13 AM
  5. Please solve my error occured java.lang.NullPointerExceptio
    By Viruthagiri in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: March 24th, 2010, 08:29 AM