Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (601)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ? in Java  (Read 5437 times)
0 Members and 1 Guest are viewing this topic.
Offline ENC

Junior Devvie





« Posted 2006-09-28 06:37:02 »

Hi all...


I have this question... I have seen many people type this

if (number1 == 100) ? break, number1++;

they say it is to reduce the number of RAM space used compared to this

if(number1 == 100){
        break;
}else{
number1++;
}

I just dont understand why is that possible to write the first line and compile it. I tried to use the same line above to make the same effect however they give me an "illegal start of expression" error

Help pls?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public class Fun1{
   private int number1;
   
   public void run(){
      number1 = 10;
      System.out.println(number1);
      while(true){
         number1++;
         System.out.println(number1);
         try{
            Thread.sleep(50);
            if (number1 == 100) ? number1--,number1++;
         }catch(InterruptedException e){}
      }
   }
   public static void main(String []args){
      new Fun1().run();
   }
}
Offline bjgil2

Senior Newbie




That's it Mr. Giraffe, get all the marmalade


« Reply #1 - Posted 2006-09-28 07:03:44 »

You need to write it like this:

(number1 == 100 ? break : number1++ )

Cheers,
Brett
Offline ENC

Junior Devvie





« Reply #2 - Posted 2006-09-28 07:51:21 »

Hey Brett

how do you put it in the code?

is it "if(number1 == 100 ? break : number1 ++);" ?

or is it just (number1 == 100 ? break : number1++ )

cause I cant seem to get it compiled

Sorry =/

Cheers!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline cylab

JGO Ninja


Medals: 55



« Reply #3 - Posted 2006-09-28 08:40:31 »

I can't think of this being compilable. the ? is part of a so called ternary operator and is IMHO only useful in assignments:
1  
2  
3  
4  
5  
6  
7  
// assingn bar to foo as long as it is less than 100, else assign constant 100
int foo= bar<100?bar:100;

// which is equivalent to the followinr
int foo;
if(bar<100) foo=bar;
else bar=100;


I wouldn't use the ternary construct in any other cases but the most simple ones, cause it leads to completely unreadable code. Also I doubt, that the ternary operator takes less space in byte code, but one never know...

Mathias - I Know What [you] Did Last Summer!
Offline tortoise

Junior Devvie




<3 Shmups


« Reply #4 - Posted 2006-09-28 21:14:38 »

you need to change it to this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public class Fun1{
   private int number1;
   
   public void run(){
      number1 = 10;
      System.out.println(number1);
      while(true){
         number1++;
         System.out.println(number1);
         try{
            Thread.sleep(50);
            number1 == 100 ? number1-- : number1++;
         }catch(InterruptedException e){}
      }
   }
   public static void main(String []args){
      new Fun1().run();
   }
}


a colon separates the "if" and "else" portions of the ternary statement.

And I highly doubt the ternary statement compiles to anything different than what an equivalent if/else statement would.
Offline Ask_Hjorth_Larsen

Junior Devvie




Java games rock!


« Reply #5 - Posted 2006-09-29 19:38:51 »

The word break is reserved, so you cannot use it. You cannot have seen the code in question in a working program.
Offline appel

JGO Wizard


Medals: 68
Projects: 4


I always win!


« Reply #6 - Posted 2006-09-30 00:34:39 »

I like the ? stuff, it allows you to write complex stuff in very few lines:

Here are a few crazy lines I've done, lol;

1  
2  
3  
P_X+=(float)Math.sin(P_ANGLE)*((((ACTIONS&A_FORWARD)==A_FORWARD))?(PLAYER_SPEED_FORWARD):(((ACTIONS&A_REVERSE)==A_REVERSE)?(-PLAYER_SPEED_REVERSE):0));
P_Y+=(float)Math.cos(P_ANGLE)*((((ACTIONS&A_FORWARD)==A_FORWARD))?(-PLAYER_SPEED_FORWARD):(((ACTIONS&A_REVERSE)==A_REVERSE)?(PLAYER_SPEED_REVERSE):0));
P_ANGLE = (float) ((P_ANGLE%(2*PI))+(((ACTIONS&A_TURNRIGHT)==A_TURNRIGHT)?PLAYER_SPEED_TURN:((ACTIONS&A_TURNLEFT)==A_TURNLEFT)?-PLAYER_SPEED_TURN:0));


I'll give you a million bucks if you can sort that out Wink

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #7 - Posted 2006-09-30 23:00:46 »

I like the ? stuff, it allows you to write complex stuff in very few lines:

That's not a good thing.

I'll give you a million bucks if you can sort that out  Wink

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
if ((ACTIONS & A_FORWARD) == A_FORWARD) // If move forward bit is set
{
    P_X += (float) Math.sin(P_ANGLE) * PLAYER_SPEED_FORWARD;
    P_Y -= (float) Math.cos(P_ANGLE) * PLAYER_SPEED_FORWARD;
}
else if ((ACTIONS & A_REVERSE) == A_REVERSE) // If reverse bit is set
{
    P_X -= (float) Math.sin(P_ANGLE) * PLAYER_SPEED_REVERSE;
    P_Y += (float) Math.cos(P_ANGLE) * PLAYER_SPEED_REVERSE;
}

if ((ACTIONS & A_TURNRIGHT) == A_TURNRIGHT) // If turn right bit is set
{
    P_ANGLE = (P_ANGLE % (2 * PI)) + PLAYER_SPEED_TURN;
}
else if ((ACTIONS & A_TURNLEFT) == A_TURNLEFT) // If turn left bit is set
{
    P_ANGLE = (P_ANGLE % (2 * PI)) - PLAYER_SPEED_TURN;
}


That code is MUCH clearer than your code, and also adds an option to let the user press both turn left and turn right at the same time by simply removing all "elses" from the code.
In case you thought your code was faster; it isn't. In a microbenchmark, my code ran about 25% faster than your code on my computer, and provided identical output(*).
Your code, however, DOES generate a slightly smaller class file. Wink

(* almost. Since I changed the order on some negations for clearer code, there's some minor differences in the double -> float casting results)

Play Minecraft!
Offline appel

JGO Wizard


Medals: 68
Projects: 4


I always win!


« Reply #8 - Posted 2006-10-01 01:36:25 »

Your code, however, DOES generate a slightly smaller class file. Wink

Yea, I created it for the (this years) 4k competition. The ?-statements help make the code smaller.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline cylab

JGO Ninja


Medals: 55



« Reply #9 - Posted 2006-10-01 03:16:24 »

Yea, I created it for the (this years) 4k competition. The ?-statements help make the code smaller.
Is it so? I never checked this myself, so there is really a dedicated bytecode in java for the ternary operator!? Well, one never stop learning...

Mathias - I Know What [you] Did Last Summer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #10 - Posted 2006-10-01 11:00:08 »

No, there's no special opcode, but since they don't REALLY do the same thing, they compile slightly differently. Sometimes the ? is smaller, sometimes the if is smaller.

Here's two examples:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
    public static int foo(boolean b)
    {
        int dir = b?1:-1;
        return dir;
    }

    public static int foo2(boolean b)
    {
        int dir;
        if (b)
            dir = 1;
        else
            dir = -1;
        return dir;
    }


decompiles to:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public static int foo(boolean);
  Code:
   0:   iload_0
   1:   ifeq    8
   4:   iconst_1
   5:   goto    9
   8:   iconst_m1
   9:   istore_1
   10:  iload_1
   11:  ireturn

public static int foo2(boolean);
  Code:
   0:   iload_0
   1:   ifeq    9
   4:   iconst_1
   5:   istore_1
   6:   goto    11
   9:   iconst_m1
   10:  istore_1
   11:  iload_1
   12:  ireturn



BUT:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
    public static int foo(boolean b)
    {
        return b ? 1 : -1;
    }

    public static int foo2(boolean b)
    {
        if (b) return 1;
        else return -1;
    }


decompiles to:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public static int foo(boolean);
  Code:
   0:   iload_0
   1:   ifeq    8
   4:   iconst_1
   5:   goto    9
   8:   iconst_m1
   9:   ireturn

public static int foo2(boolean);
  Code:
   0:   iload_0
   1:   ifeq    6
   4:   iconst_1
   5:   ireturn
   6:   iconst_m1
   7:   ireturn


So really, you need to test it in your app to see what's smallest, or use a different compiler that's smarter than javac. Wink
It's trivial to prove that all those four methods do the same thing, yet javac produces four different bytecodes for them.


(by the way, to decompile a class file, use "javap -c -classpath <path> my.package.ClassName")

Play Minecraft!
Offline appel

JGO Wizard


Medals: 68
Projects: 4


I always win!


« Reply #11 - Posted 2006-10-01 16:13:23 »

Yes, there is almost no difference.

8 instructions vs. 9 instructions
and then 6 v 6 instructions.

Quote
Sometimes the ? is smaller, sometimes the if is smaller.

btw. the ternary operator is not larger than the if-operator. Count the instructions, don't just look at the line no.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Markus_Persson

JGO Wizard


Medals: 16
Projects: 19


Mojang Specifications


« Reply #12 - Posted 2006-10-01 17:10:13 »

Count the instructions, don't just look at the line no.

That's not line numbers. It's the actual numbers of bytes used for the bytecode.
goto, for example, takes up three bytes (opcode, offset high byte, offset low byte). Sure, it's a single instruction, but it makes the byte code three bytes larger.

Play Minecraft!
Offline appel

JGO Wizard


Medals: 68
Projects: 4


I always win!


« Reply #13 - Posted 2006-10-01 17:46:06 »

Count the instructions, don't just look at the line no.

That's not line numbers. It's the actual numbers of bytes used for the bytecode.
goto, for example, takes up three bytes (opcode, offset high byte, offset low byte). Sure, it's a single instruction, but it makes the byte code three bytes larger.


I see.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline ENC

Junior Devvie





« Reply #14 - Posted 2006-10-08 01:40:30 »

Hmm... I understand the theory part of ? However.. what I do not understand is that.. i still cannot compile the codes

even this codes which is provided for me

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
public class Fun1{
   private int number1;
   
   public void run(){
      number1 = 10;
      System.out.println(number1);
      while(true){
         number1++;
         System.out.println(number1);
         try{
            Thread.sleep(50);
            number1 == 100 ? number1-- : number1++;
         }catch(InterruptedException e){}
      }
   }
   public static void main(String []args){
      new Fun1().run();
   }
}


It always gives me a "not a statement" error at the line "number1 == 100 ? number1-- : number1++;"

I really do not understand.. I installed the latest JDK.. and I still have this error... I really do not know what is going on...

=(
Offline bjgil2

Senior Newbie




That's it Mr. Giraffe, get all the marmalade


« Reply #15 - Posted 2006-10-08 02:16:25 »

Replace the line you have with:

number1 = (number1 == 100 ? number1 - 1 : number1 + 1);

You can't use the ? operator in isolation, it has to be part of a statement.

Cheers,
Brett
Offline appel

JGO Wizard


Medals: 68
Projects: 4


I always win!


« Reply #16 - Posted 2006-10-08 02:49:24 »

1  
number1 == 100 ? number1-- : number1++;


This is most illogical. The number "100" is a integer, it cannot be true or false.
Also, this requires incrementing or decrementing number1 variable by one, and then assign value of number1 to number1, which is not necessery.

Better:
1  
number1 += (number1 == 100) ? -1 : 1;

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline ENC

Junior Devvie





« Reply #17 - Posted 2006-10-08 09:25:49 »

Thanks! I undestand ? in Java...

HOwever I would like to ask.. is there any other special characters like ? used in java and what are they used for and how they are used?

^^

CHeers!
Offline tortoise

Junior Devvie




<3 Shmups


« Reply #18 - Posted 2006-10-10 18:54:22 »

I like the ? stuff, it allows you to write complex stuff in very few lines:

Here are a few crazy lines I've done, lol;

1  
2  
3  
P_X+=(float)Math.sin(P_ANGLE)*((((ACTIONS&A_FORWARD)==A_FORWARD))?(PLAYER_SPEED_FORWARD):(((ACTIONS&A_REVERSE)==A_REVERSE)?(-PLAYER_SPEED_REVERSE):0));
P_Y+=(float)Math.cos(P_ANGLE)*((((ACTIONS&A_FORWARD)==A_FORWARD))?(-PLAYER_SPEED_FORWARD):(((ACTIONS&A_REVERSE)==A_REVERSE)?(PLAYER_SPEED_REVERSE):0));
P_ANGLE = (float) ((P_ANGLE%(2*PI))+(((ACTIONS&A_TURNRIGHT)==A_TURNRIGHT)?PLAYER_SPEED_TURN:((ACTIONS&A_TURNLEFT)==A_TURNLEFT)?-PLAYER_SPEED_TURN:0));


I'll give you a million bucks if you can sort that out Wink


Oh God. Please let me never work on a project with you, and please let no one who writes code like this ever get near me Smiley
Offline tortoise

Junior Devvie




<3 Shmups


« Reply #19 - Posted 2006-10-10 18:57:11 »

Thanks! I undestand ? in Java...

HOwever I would like to ask.. is there any other special characters like ? used in java and what are they used for and how they are used?

^^

CHeers!

You mean punctuation characters? Sure, there are plenty. Almost all of them you are well aware of (I sure hope you know what the semi colon is used for, for example)

Maybe one you're not thinking of is in Java 5, the new "foreach" loop which uses a colon

for(Foo foo : myFoos) {
    foo.doSomething();
}

it would have been nicer if that loop could have been

foreach(Foo foo in myFoos) {
...
}

as it is in C#, but introducing new keywords to a language is risky, and so Sun decided to not go that route. If nothing else, old programs that had "foreach" and "in" as variable names would have failed to compile.
Offline Abuse

JGO Knight


Medals: 14


falling into the abyss of reality


« Reply #20 - Posted 2006-10-10 20:06:02 »

Thanks! I undestand ? in Java...

HOwever I would like to ask.. is there any other special characters like ? used in java and what are they used for and how they are used?

^^

CHeers!

You mean punctuation characters? Sure, there are plenty. Almost all of them you are well aware of (I sure hope you know what the semi colon is used for, for example)

Maybe one you're not thinking of is in Java 5, the new "foreach" loop which uses a colon

for(Foo foo : myFoos) {
    foo.doSomething();
}

it would have been nicer if that loop could have been

foreach(Foo foo in myFoos) {
...
}

as it is in C#, but introducing new keywords to a language is risky, and so Sun decided to not go that route. If nothing else, old programs that had "foreach" and "in" as variable names would have failed to compile.

Infact, it'd probably be easier to list the punctuation characters that don't have a special meaning!
The only common ones I can think of that have no meaning in Java, and are not valid characters in an identifier, are these 3:-   #¬`

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline ENC

Junior Devvie





« Reply #21 - Posted 2006-10-13 16:38:41 »

Hmm... i c

Thanks! ^^
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (48 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (103 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!