Jump to content

The Ternary and Switch Statement


Bot

Recommended Posts

(This is my second time typing this entire thread)

 

The Switch Statement

 

Part of this knowledge comes from: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

The switch statement is a cleaner, faster way to show multiple if else statements. In a switch block, a  Character, Byte, Short, Integer, String or Enum can be used. Here is an example using String 

switch(myString) {    case "A":    break;    case "B":    break;}

Enums use the Enum constants when switching. For example:

switch(myEnum){     case MEnum.Foo;    break;    case MEnum.ooF;    break;}

 

Note that the only time the cases are enclosed within quotation marks( " ) is when we are using a String. Also notice the fact that we use a break after every case. This is to prevent falling through to the next case. For example 

switch(myString) {    case "A":    case "B":     break;}

 

In the case of A, not only will everything under the case of A be executed, but so will everything following that unless it is broken with a break

 

 

Extra Information from thiefmn6092:



once the number of conditions goes over a certain threshold (3 i believe with oracles jvm), a switch statement will be evaluated as a http://en.wikipedia.org/wiki/Branch_table. there are also other conditions that determine whether this is used. this allows the jvm to optimize more selectively for that block of code as well.

The Ternary Operator

 

You can consult: http://alvinalexander.com/java/edu/pj/pj010018 if you find this unclear. However, if you do please let me know. 

 

The ternary is another way of replacing an if else statement that might look like

if(myInt == 2) {   someInt = 5;}

 

In a ternary, this would look like 

someInt = myInt == 2 ? 5 : someInt;

 

Here's a breakdown of what just happened. The structure of a basic ternary is 

field = (Condition) ? (Action) : (Default Action/What happens if the condition is failed)

 

The condition is any condition that you would normally meet in an if statement. If you have an if statement like this 

if(myInt == 2 || myInt == 4 || myInt == 6) {   someInt = 5;}

 

Then the ternary would look like 

someInt = myInt == 2 || myInt == 4 || myInt == 6 ? 5 : someInt;

 

You can also stack multiple ternaries into one statement so where the default action would be we can say 

someInt = myInt == 2 || myInt == 4 || myInt == 6 ? 5 : myInt == 8 ? 10 : -1;

 

That is the same as saying
 

if(myInt == 2 || myInt == 4 || myInt == 6) {   someInt = 5;} else if(myInt == 8) {   someInt = 10;} else {   someInt = -1;}

 

The ternary just puts it into one neat little line. Of course you can apply your java syntax knowledge and use brackets.

 

Let me know if I missed anything, this is my second attempt so I'm kind of worn. 

Edited by Bot
Link to comment
Share on other sites

Guest mainniam

once the number of conditions goes over a certain threshold (3 i believe with oracles jvm), a switch statement will be evaluated as a http://en.wikipedia.org/wiki/Branch_table. there are also other conditions that determine whether this is used. this allows the jvm to optimize more selectively for that block of code as well.

Edited by thiefmn6092
  • Like 2
Link to comment
Share on other sites

I've just skimmed the top and already spotted 2 mistakes. You should make sure everything in a tutorial is perfect, so as to not teach others bad habits.

 

Enum types should be named in full caps. I know that was just a demonstration but yea people might copy you.

 

You should also mention that string switch support was added in Java7, so if they use that their program won't be useable by anyone using prior versions.

Link to comment
Share on other sites

Guest mainniam

I've just skimmed the top and already spotted 2 mistakes. You should make sure everything in a tutorial is perfect, so as to not teach others bad habits.

 

Enum types should be named in full caps. I know that was just a demonstration but yea people might copy you.

 

all that is is a convention choice... it's not enforced by the language at all.

Link to comment
Share on other sites

I've just skimmed the top and already spotted 2 mistakes. You should make sure everything in a tutorial is perfect, so as to not teach others bad habits.

 

Enum types should be named in full caps. I know that was just a demonstration but yea people might copy you.

 

You should also mention that string switch support was added in Java7, so if they use that their program won't be useable by anyone using prior versions.

 

Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants.

Link to comment
Share on other sites

Yea, I meant the instances.

 

Also when using enums in a switch, you don't have to reference the enum, you can just use the instance name.

 

 

 

all that is is a convention choice... it's not enforced by the language at all.

 
Obviously.. It's generally recommended to follow the conventions, didn't you know?
Edited by Sync
Link to comment
Share on other sites

Honestly, Ternary operators look messy, not sure if I'm not used to them or they just do, I'd rather stick with if else statements, they're cleaner imo, more lines of code, but I like em, thanks for the share though, if I feel comfortable, I may even use those.

 

Tip: You should compare if else statement with switch statement, I didn't understand it until I read it over again.

Link to comment
Share on other sites

It all depends on the situation, I think ternary is very usefull to clean up your code in most cases.

Following example would be alot messier if using if else statements:

	public static Area createCubicArea(Position p1, Position p2) {		int minX = (p1.getX() < p2.getX()) ? p1.getX() : p2.getX();		int maxX = (p1.getX() > p2.getX()) ? p1.getX() : p2.getX();		int minY = (p1.getY() < p2.getY()) ? p1.getY() : p2.getY();		int maxY = (p1.getY() > p2.getY()) ? p1.getY() : p2.getY();		int minZ = (p1.getZ() < p2.getZ()) ? p1.getZ() : p2.getZ();		int maxZ = (p1.getZ() > p2.getZ()) ? p1.getZ() : p2.getZ();				Area cubicArea = new Area();				for(int i = 0; i <= (maxX - minX); i++) {			for(int j = 0; j <= (maxY - minY); j++) {				for(int k = 0; k <= (maxZ - minZ); k++) {					cubicArea.add(new Position(minX +i, minY +j, minZ +k));				}			}		}				return cubicArea;	}
Edited by KevinHouse
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...