public enum searchengines{Google, Yahoo, Bing, Exhead, Baidu};The comma at the end is optional, both writes are legal.
enginesSearch engine = search engines.Google;All enumerations inherit from the class java.lang.Enum because a class can only inherit from one parent but from multiple interfaces.
for( Search Engines Engine: searchengines.values()) {
System.out.println(engine);
}
public class EnumExample {Output:
public enum searchengines{
Google(160), Yahoo(66.4f), Bing(7.f), Baidu(66), Ask(4.6f);
private float visitors;
private searchengines(float visitors) {
this.visitors = visitors;
}
}
public static void main(String[] args) {
for( search engines: searchengines.values()) {
System.out.println("Search engine: "+engine+"
visitors: "+engine.visitors+" millions");
}
}
}
Search engine: Google visitors: 160.0 millionConstants are declared static and final and cannot be changed once they are created.
Search engine: Yahoo visitors: 66.4 million
Search engine: Bing visitors: 7.0 million
Search engine: Baidu visitors: 66.0 million
Search engine: Ask visitors: 4.6 million
google search engines = searchengines.Google;
if(google.equals(GoogleSearchengines))
System.out.println("comparison with equals");
if(google == searchengines.Google)
System.out.println("comparison with ==");
public class EnumExample{
public enum searchengines{
Google(160), Yahoo(66.4f), Bing(7.f), Baidu(66), Ask(4.6f);
private float visitors;
private searchengines(float visitors) {
this.visitors = visitors;
}
}
Searchengines name;
EnumExample(Searchengines name){
this.name=name;
}
public void slogan() {
switch (name) {
Google:
System.out.println("Don't be malicious, don't do evil");
estate;
box Yahoo:
System.out.println("Yahoo! It's You. Do you Yahoo?");
estate;
box Bing:
System.out.println("Bing is for doing.");
estate;
box Baidu:
System.out.println("Baidu knows Chinese better");
estate;
box Ask:
System.out.println("What's your question?");
estate;
}
}
public static void main(String[] args) {
Google Example = new ExampleNumber(SearchEngines.Google);
google.slogan();
Example enum yahoo = new Example Enum(Searchengines.Yahoo);
yahoo.slogan();
Example Num bing = new SampleNum(SearchEngines.Bing);
bing.slogan();
Baidu Example Enum = new Baidu Example Enum(SearchEngines.Baidu);
baidu.slogan();
EnumExample ask = new EnumExample(Searchengines.Ask);
ask.slogan();
}
}
toString returns the name of the constant:The function valueOf() does the reverse processing of the method toString(), we give it the name and it returns the constant:
System.out.println(yahoo.toString());
System.out.println(yahoo.valueOf("Yahoo"));
System.out.println(google.compareTo(yahoo));
Please disable your ad blocker and refresh the window to use this website.