Files
JavaIntermediate/castingandothers/src/main/java/com/example/Main.java
2024-10-05 18:05:18 +02:00

73 lines
2.0 KiB
Java

package com.example;
import java.util.Scanner;
public class Main {
enum Rank {
SOLDIER,
SERGEANT,
CAPTAIN
}
public static void main(String[] args) {
System.out.println("Hello world!");
double asd = 3.5423;
int castedasd = (int) asd;
System.out.println(castedasd);
System.out.print("Gimme a letter and Ill tel you the ASCII code! ");
Scanner read = new Scanner(System.in);
char a = read.next().charAt(0);
//your code goes here
System.out.println((int)a);
// Upcasting
// You can cast an instance of a subclass to its superclass.
// Consider the following example, assuming that Cat is a subclass of Animal.
Animal C = new Cat();
Animal B = new Cat();
System.out.println(((Cat)B).sound);
// Anonymous Classes
// Anonymous classes are a way to extend the existing classes on the fly.
// For example, consider having a class Machine:
Cat c = new Cat(){
@Override public void makesound(){
System.err.println("MOOOOOOOOOOOOOO");
}
};
c.makesound();
//ENUMS
Rank aa = Rank.SOLDIER;
switch(aa) {
case SOLDIER:
System.out.println("Soldier says hi!");
break;
case SERGEANT:
System.out.println("Sergeant says Hello!");
break;
case CAPTAIN:
System.out.println("Captain says Welcome!");
break;
}
// Java API
// The Java API is a collection of classes and interfaces that have been written for you to use.
// The Java API Documentation with all of the available APIs can be located on the Oracle website at
// http://docs.oracle.com/javase/7/docs/api/
// Once you locate the package you want to use, you need to import it into your code.
}
}