Apr 16

What is the Output of the following Code?

enum ShopOwner{Mary, Steve, Brian};

class Shop{

public static void main (String args []){

ShopOwner a = ShopOwner.Mary;
String b = new String(“Mary”);

System.out.println(a.equals(“Mary”));
System.out.println(a.equals(b));
System.out.println(a.equals(ShopOwner.Mary));
System.out.println(a==ShopOwner.Mary);

}

}

Antwort/ answer
Tagged with:
Apr 15

What is the output of the following code?

enum ShopNumber{1,2,3};

class Shop{

public static void main (String args []){

ShopNumber a = ShopNumber.1;
System.out.println(a);

}

}

Antwort/ answer
Tagged with:
Apr 14

A Compiler-Error is caused at which line?

enum ShopOwner{ Mary, Steve, Brian}

class Shop{

public static void main (String args []){

ShopOwner a = ShopOwner.Mary;

switch(a){ //1

case “Mary”: System.out.println(“Mary”); //2
case Steve: System.out.println(“Steve”); //3
case ShopOwner.Brian: System.out.println(“Brian”); //4

}

}

}

Antwort/ answer
Tagged with:
Apr 13

For iteration we can use method values(); to get the order we use ordinal()

enum ShopOwner{ Mary, Steve, Brian}

class Shop{

public static void main (String args []){

for (ShopOwner x:ShopOwner.values())

System.out.println(x.ordinal()+”: “+x);

}

}

Output is therefore:

0: Mary
1: Steve
2: Brian

Tagged with:
Apr 12

The general difference in comparison == (comparison of pointer) and equals (comparison of value) will not be applied for enums.

enum ShopOwner{ Mary, Steve, Brian}

class Shop{

public static void main (String args []){

ShopOwner a = ShopOwner.Mary;
ShopOwner b = ShopOwner.Mary;

System.out.println(a==b);
System.out.println(a.equals(b));

}

}

Therefore the output will be

true
true

…but be careful:

System.out.println(a.equals(“Mary”));

will result in the output

false

Tagged with:
Apr 11

Similar to anonymous inner classes also methods can be overwritten for special enums:

enum ShopOwner{

Mary(“NewYork”), Steve(“SanFrancisco”), Brian(“Chicago”){

public String getTown(){

return “Chicago, St. Peters Street”;

}

}; // method overwritten for enum-value “Brian” … and only for him :)

ShopOwner(String town){ this.town=town; }

private String town;

public String getTown(){return town;} // enum-method

}

class Shop{

public static void main (String args []){

ShopOwner a = ShopOwner.Mary;

System.out.println(a.getTown()); // print town which is dedicated to Mary

}

}

Tagged with:
Apr 10

Constructors  can not be invoked by code, it is always invoked automatically, eg weekdays a = weekdays.MONDAY or . There is no possibility to invoke a constructor on another way.

Also the new-operator can not be applied for enums.

As already explained a constructor can be overloaded, if preferred even with more than one argument.

Tagged with:
Apr 09

It is also possible to overload the enum-constructor:

enum ShopOwner{

Mary(“NewYork”), Steve(“SanFrancisco”), Brian(“Chicago”);

ShopOwner(String town){ this.town=town; }

private String town;

public String getTown(){return town;} // enum-method

}

class Shop{

public static void main (String args []){

ShopOwner a = ShopOwner.Mary;

System.out.println(a.getTown()); // print town which is dedicated to Mary

}

}

In our example to every shop owner a dedicated town will be assigned.

Tagged with:
Apr 08

So far we know already a little bit how to use enums and for which purpose. So let me give you a little bit of information how are enums seen in the background:

Enums are not comparable with variables or Objects; they are not type of String, int or whatever. In our example

enum weekdays {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY}

Monday is from type weekdays – nothing else.

Enums are always static and final. Every enum-value has (similar to Arrays) its own position/index. I will explain how to access and iterate through them within the next days.

Tagged with:
Apr 07

To confuse you even more the ‘;’ is optional for enum declaration

enum weekdays {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY}

is therefore as well valid as

enum weekdays {MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY};

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs