Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: conditional initialization?

Ruby has conditional initialization. Apparently, Java does not or does it? I try to write more succintly, to limit the range as small as possible.

import java.io.*;
import java.util.*;

public class InitFor{

        public static void main(String[] args){
                for(int i=7,k=999;i+((String h="hello").size())<10;i++){}

                System.out.println("It should be: hello = "+h);

        }
}

Errors

Press ENTER or type command to continue
InitFor.java:8: ')' expected
  for(int i=7,k=999;i+((String h="hello").size())<10;i++){}
                              ^

Puzzles

  1. Is it possible to assign a value to an declared value in while-loop? YES code1
  2. Assignment in for-loop conditional? YES code2
  3. conditional init NO
  4. Can you assign different types of values in loops? YES in a reply
  5. Some rule for initialization inside loops? Declare outside to access values later, what about init? (?)

1. CODE

import java.io.*;
import java.util.*;

public class InitFor{
        public static void main(String[] args){
                int k=5;
                while((k=(k%3)+1)!=1){
                        System.out.println(k);
                }
                //PRINTs only 3
        }
}

2. CODE

import java.io.*;
import java.util.*;

public class InitFor{

        public static void main(String[] args){
                int k=5;
                for(;(k=(k%3)+1)!=1;){
                        System.out.println(k);
                }
                //PRINTs only 3
                System.out.println(k); 
                // WHY DOES IT PRINT 1? Assign in for-loop!
        }
}

Part of the Original problem with many different kind of assignments and initializations -- 100lines of code in one-liner

for(int t=0,size=(File[] fs=((File f=f.getParentFile()).listFiles(filt))).size();fs==null;t++){if(t>maxDepth){throw new Exception("No dir to read");}}
like image 289
hhh Avatar asked Jan 22 '23 04:01

hhh


2 Answers

The problem is that a variable may not be declared there; variables may be declared within a block, but may not be declared as part of an expression. However, you can allocate objects in an expression:

for(int i=7,k=999;i+((new String("hello")).length())<10;i++){}

For your program, the following would make more sense:

public static void main(String[] args){
    String h = "hello";
    for(int i=7,k=999;(i+h.length())<10;i++){} // not sure what this is doing
    System.out.println("It should be: hello = "+h);
}

I would also add that even if a declaration were allowed where you had it, the variable would belong to the scope of the for-loop, and so the variable would no longer be visible after the loop (out of the scope).

like image 174
Michael Aaron Safyan Avatar answered Jan 24 '23 17:01

Michael Aaron Safyan


Variable declarations cannot be parts of expressions, they are statements, as the Java spec said so.

If conditional initialization existed in Java, then how to determine if a variable is initialized? How to compile the code properly? (you need to understand how do Java compiler work to know it is impossible) How are errors handled? There are many complications. In your code, even if the variable is initialized, it will be gone after the for block due to Java's scoping strategy.

like image 27
Ming-Tang Avatar answered Jan 24 '23 16:01

Ming-Tang