mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-11 01:04:10 +02:00
[java/en] few changes (#2788)
This commit is contained in:
@@ -50,7 +50,7 @@ public class LearnJava {
|
|||||||
|
|
||||||
// In order to run a java program, it must have a main method as an entry
|
// In order to run a java program, it must have a main method as an entry
|
||||||
// point.
|
// point.
|
||||||
public static void main (String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
// Input/Output
|
// Input/Output
|
||||||
@@ -120,7 +120,8 @@ public class LearnJava {
|
|||||||
// Initialize a variable using <type> <name> = <val>
|
// Initialize a variable using <type> <name> = <val>
|
||||||
int barInt = 1;
|
int barInt = 1;
|
||||||
// Initialize multiple variables of same type with same
|
// 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;
|
int barInt1, barInt2, barInt3;
|
||||||
barInt1 = barInt2 = barInt3 = 1;
|
barInt1 = barInt2 = barInt3 = 1;
|
||||||
|
|
||||||
@@ -237,7 +238,7 @@ public class LearnJava {
|
|||||||
// StringBuilder is efficient when the fully constructed String is not required until the end of some processing.
|
// StringBuilder is efficient when the fully constructed String is not required until the end of some processing.
|
||||||
StringBuilder stringBuilder = new StringBuilder();
|
StringBuilder stringBuilder = new StringBuilder();
|
||||||
String inefficientString = "";
|
String inefficientString = "";
|
||||||
for(int i = 0 ; i < 10; i++){
|
for (int i = 0 ; i < 10; i++) {
|
||||||
stringBuilder.append(i).append(" ");
|
stringBuilder.append(i).append(" ");
|
||||||
inefficientString += i + " ";
|
inefficientString += i + " ";
|
||||||
}
|
}
|
||||||
@@ -416,6 +417,7 @@ public class LearnJava {
|
|||||||
// It also works with enumerated types (discussed in Enum Types), the
|
// It also works with enumerated types (discussed in Enum Types), the
|
||||||
// String class, and a few special classes that wrap primitive types:
|
// String class, and a few special classes that wrap primitive types:
|
||||||
// Character, Byte, Short, and Integer.
|
// Character, Byte, Short, and Integer.
|
||||||
|
// Starting in Java 7 and above, we can also use the String type.
|
||||||
int month = 3;
|
int month = 3;
|
||||||
String monthString;
|
String monthString;
|
||||||
switch (month) {
|
switch (month) {
|
||||||
@@ -430,23 +432,6 @@ public class LearnJava {
|
|||||||
}
|
}
|
||||||
System.out.println("Switch Case Result: " + monthString);
|
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-with-resources (Java 7+)
|
||||||
// Try-catch-finally statements work as expected in Java but in Java 7+
|
// Try-catch-finally statements work as expected in Java but in Java 7+
|
||||||
@@ -456,7 +441,7 @@ public class LearnJava {
|
|||||||
|
|
||||||
// In order to use a try-with-resources, include an instance of a class
|
// In order to use a try-with-resources, include an instance of a class
|
||||||
// in the try statement. The class must implement java.lang.AutoCloseable.
|
// in the try statement. The class must implement java.lang.AutoCloseable.
|
||||||
try(BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) {
|
try (BufferedReader br = new BufferedReader(new FileReader("foo.txt"))) {
|
||||||
// You can attempt to do something that could throw an exception.
|
// You can attempt to do something that could throw an exception.
|
||||||
System.out.println(br.readLine());
|
System.out.println(br.readLine());
|
||||||
// In Java 7, the resource will always be closed, even if it throws
|
// In Java 7, the resource will always be closed, even if it throws
|
||||||
@@ -481,7 +466,7 @@ public class LearnJava {
|
|||||||
System.out.println(bar); // Prints A, because the statement is true
|
System.out.println(bar); // Prints A, because the statement is true
|
||||||
|
|
||||||
////////////////////////////////////////
|
////////////////////////////////////////
|
||||||
// Converting Data Types And Typecasting
|
// Converting Data Types
|
||||||
////////////////////////////////////////
|
////////////////////////////////////////
|
||||||
|
|
||||||
// Converting data
|
// Converting data
|
||||||
@@ -497,11 +482,6 @@ public class LearnJava {
|
|||||||
// Long
|
// Long
|
||||||
// String
|
// 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
|
// 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
|
// Interfaces
|
||||||
// Interface declaration syntax
|
// Interface declaration syntax
|
||||||
// <access-level> interface <interface-name> extends <super-interfaces> {
|
// <access-level> interface <interface-name> extends <super-interfaces> {
|
||||||
@@ -667,10 +655,10 @@ public interface Edible {
|
|||||||
|
|
||||||
public interface Digestible {
|
public interface Digestible {
|
||||||
public void digest();
|
public void digest();
|
||||||
// In Java 8, interfaces can have default method.
|
// Since Java 8, interfaces can have default method.
|
||||||
// public void digest() {
|
public void defaultMethod() {
|
||||||
// System.out.println("digesting ...");
|
System.out.println("Hi from default method ...");
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// We can now create a class that implements both of these interfaces.
|
// 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 Classes
|
||||||
|
|
||||||
// Abstract Class declaration syntax
|
// 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
|
// // Constants and variables
|
||||||
// // Method declarations
|
// // Method declarations
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// Marking a class as abstract means that it contains abstract methods that
|
// Marking a class as abstract means that it contains at least one abstract
|
||||||
// must be defined in a child class. Similar to interfaces, abstract classes
|
// method that must be defined in a child class. Similar to interfaces, abstract
|
||||||
// cannot be instantiated, but instead must be extended and the abstract
|
// classes cannot be instantiated, but instead must be extended and the abstract
|
||||||
// methods defined. Different from interfaces, abstract classes can contain a
|
// methods defined. Different from interfaces, abstract classes can contain a
|
||||||
// mixture of concrete and abstract methods. Methods in an interface cannot
|
// 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,
|
// have a body, unless the method is static, and variables are final by default,
|
||||||
|
Reference in New Issue
Block a user