mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 17:41:41 +00:00
[java/en] few changes (#2788)
This commit is contained in:
parent
b67ac8da07
commit
4a359c303f
@ -120,7 +120,8 @@ public class LearnJava {
|
||||
// Initialize a variable using <type> <name> = <val>
|
||||
int barInt = 1;
|
||||
// Initialize multiple variables of same type with same
|
||||
// value <type> <name1>, <name2>, <name3> = <val>
|
||||
// value <type> <name1>, <name2>, <name3>
|
||||
// <name1> = <name2> = <name3> = <val>
|
||||
int barInt1, barInt2, barInt3;
|
||||
barInt1 = barInt2 = barInt3 = 1;
|
||||
|
||||
@ -416,6 +417,7 @@ public class LearnJava {
|
||||
// It also works with enumerated types (discussed in Enum Types), the
|
||||
// String class, and a few special classes that wrap primitive types:
|
||||
// Character, Byte, Short, and Integer.
|
||||
// Starting in Java 7 and above, we can also use the String type.
|
||||
int month = 3;
|
||||
String monthString;
|
||||
switch (month) {
|
||||
@ -430,23 +432,6 @@ public class LearnJava {
|
||||
}
|
||||
System.out.println("Switch Case Result: " + monthString);
|
||||
|
||||
// Starting in Java 7 and above, switching Strings works like this:
|
||||
String myAnswer = "maybe";
|
||||
switch(myAnswer) {
|
||||
case "yes":
|
||||
System.out.println("You answered yes.");
|
||||
break;
|
||||
case "no":
|
||||
System.out.println("You answered no.");
|
||||
break;
|
||||
case "maybe":
|
||||
System.out.println("You answered maybe.");
|
||||
break;
|
||||
default:
|
||||
System.out.println("You answered " + myAnswer);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// Try-with-resources (Java 7+)
|
||||
// Try-catch-finally statements work as expected in Java but in Java 7+
|
||||
@ -481,7 +466,7 @@ public class LearnJava {
|
||||
System.out.println(bar); // Prints A, because the statement is true
|
||||
|
||||
////////////////////////////////////////
|
||||
// Converting Data Types And Typecasting
|
||||
// Converting Data Types
|
||||
////////////////////////////////////////
|
||||
|
||||
// Converting data
|
||||
@ -497,11 +482,6 @@ public class LearnJava {
|
||||
// Long
|
||||
// String
|
||||
|
||||
// Typecasting
|
||||
// You can also cast Java objects, there's a lot of details and deals
|
||||
// with some more intermediate concepts. Feel free to check it out here:
|
||||
// https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
|
||||
|
||||
///////////////////////////////////////
|
||||
// Classes And Functions
|
||||
///////////////////////////////////////
|
||||
@ -652,6 +632,14 @@ class PennyFarthing extends Bicycle {
|
||||
}
|
||||
}
|
||||
|
||||
// Object casting
|
||||
// Since the PennyFarthing class is extending the Bicycle class, we can say
|
||||
// a PennyFarthing is a Bicycle and write :
|
||||
// Bicycle bicycle = new PennyFarthing();
|
||||
// This is called object casting where an object is taken for another one. There
|
||||
// are lots of details and deals with some more intermediate concepts here:
|
||||
// https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
|
||||
|
||||
// Interfaces
|
||||
// Interface declaration syntax
|
||||
// <access-level> interface <interface-name> extends <super-interfaces> {
|
||||
@ -667,10 +655,10 @@ public interface Edible {
|
||||
|
||||
public interface Digestible {
|
||||
public void digest();
|
||||
// In Java 8, interfaces can have default method.
|
||||
// public void digest() {
|
||||
// System.out.println("digesting ...");
|
||||
// }
|
||||
// Since Java 8, interfaces can have default method.
|
||||
public void defaultMethod() {
|
||||
System.out.println("Hi from default method ...");
|
||||
}
|
||||
}
|
||||
|
||||
// We can now create a class that implements both of these interfaces.
|
||||
@ -703,14 +691,15 @@ public class ExampleClass extends ExampleClassParent implements InterfaceOne,
|
||||
// Abstract Classes
|
||||
|
||||
// Abstract Class declaration syntax
|
||||
// <access-level> abstract <abstract-class-name> extends <super-abstract-classes> {
|
||||
// <access-level> abstract class <abstract-class-name> extends
|
||||
// <super-abstract-classes> {
|
||||
// // Constants and variables
|
||||
// // Method declarations
|
||||
// }
|
||||
|
||||
// Marking a class as abstract means that it contains abstract methods that
|
||||
// must be defined in a child class. Similar to interfaces, abstract classes
|
||||
// cannot be instantiated, but instead must be extended and the abstract
|
||||
// Marking a class as abstract means that it contains at least one abstract
|
||||
// method that must be defined in a child class. Similar to interfaces, abstract
|
||||
// classes cannot be instantiated, but instead must be extended and the abstract
|
||||
// methods defined. Different from interfaces, abstract classes can contain a
|
||||
// mixture of concrete and abstract methods. Methods in an interface cannot
|
||||
// have a body, unless the method is static, and variables are final by default,
|
||||
|
Loading…
Reference in New Issue
Block a user