mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-01-17 21:49:22 +01:00
[java/en] few changes (#2788)
This commit is contained in:
parent
b67ac8da07
commit
4a359c303f
@ -48,9 +48,9 @@ import java.security.*;
|
|||||||
// as the file.
|
// as the file.
|
||||||
public class LearnJava {
|
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
|
||||||
@ -109,7 +109,7 @@ public class LearnJava {
|
|||||||
*/
|
*/
|
||||||
// Declare a variable using <type> <name>
|
// Declare a variable using <type> <name>
|
||||||
int fooInt;
|
int fooInt;
|
||||||
// Declare multiple variables of the same
|
// Declare multiple variables of the same
|
||||||
// type <type> <name1>, <name2>, <name3>
|
// type <type> <name1>, <name2>, <name3>
|
||||||
int fooInt1, fooInt2, fooInt3;
|
int fooInt1, fooInt2, fooInt3;
|
||||||
|
|
||||||
@ -119,8 +119,9 @@ 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;
|
||||||
|
|
||||||
@ -130,7 +131,7 @@ public class LearnJava {
|
|||||||
// Byte - 8-bit signed two's complement integer
|
// Byte - 8-bit signed two's complement integer
|
||||||
// (-128 <= byte <= 127)
|
// (-128 <= byte <= 127)
|
||||||
byte fooByte = 100;
|
byte fooByte = 100;
|
||||||
|
|
||||||
// If you would like to interpret a byte as an unsigned integer
|
// If you would like to interpret a byte as an unsigned integer
|
||||||
// then this simple operation can help
|
// then this simple operation can help
|
||||||
int unsignedIntLessThan256 = 0xff & fooByte;
|
int unsignedIntLessThan256 = 0xff & fooByte;
|
||||||
@ -184,12 +185,12 @@ public class LearnJava {
|
|||||||
// integers longer than 64-bits. Integers are stored as an array of
|
// integers longer than 64-bits. Integers are stored as an array of
|
||||||
// of bytes and are manipulated using functions built into BigInteger
|
// of bytes and are manipulated using functions built into BigInteger
|
||||||
//
|
//
|
||||||
// BigInteger can be initialized using an array of bytes or a string.
|
// BigInteger can be initialized using an array of bytes or a string.
|
||||||
BigInteger fooBigInteger = new BigInteger(fooByteArray);
|
BigInteger fooBigInteger = new BigInteger(fooByteArray);
|
||||||
|
|
||||||
// BigDecimal - Immutable, arbitrary-precision signed decimal number
|
// BigDecimal - Immutable, arbitrary-precision signed decimal number
|
||||||
//
|
//
|
||||||
// A BigDecimal takes two parts: an arbitrary precision integer
|
// A BigDecimal takes two parts: an arbitrary precision integer
|
||||||
// unscaled value and a 32-bit integer scale
|
// unscaled value and a 32-bit integer scale
|
||||||
//
|
//
|
||||||
// BigDecimal allows the programmer complete control over decimal
|
// BigDecimal allows the programmer complete control over decimal
|
||||||
@ -199,7 +200,7 @@ public class LearnJava {
|
|||||||
// BigDecimal can be initialized with an int, long, double or String
|
// BigDecimal can be initialized with an int, long, double or String
|
||||||
// or by initializing the unscaled value (BigInteger) and scale (int).
|
// or by initializing the unscaled value (BigInteger) and scale (int).
|
||||||
BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt);
|
BigDecimal fooBigDecimal = new BigDecimal(fooBigInteger, fooInt);
|
||||||
|
|
||||||
// Be wary of the constructor that takes a float or double as
|
// Be wary of the constructor that takes a float or double as
|
||||||
// the inaccuracy of the float/double will be copied in BigDecimal.
|
// the inaccuracy of the float/double will be copied in BigDecimal.
|
||||||
// Prefer the String constructor when you need an exact value.
|
// Prefer the String constructor when you need an exact value.
|
||||||
@ -231,13 +232,13 @@ public class LearnJava {
|
|||||||
builderConcatenated.append("You ");
|
builderConcatenated.append("You ");
|
||||||
builderConcatenated.append("can use ");
|
builderConcatenated.append("can use ");
|
||||||
builderConcatenated.append("the StringBuilder class.");
|
builderConcatenated.append("the StringBuilder class.");
|
||||||
System.out.println(builderConcatenated.toString()); // only now is the string built
|
System.out.println(builderConcatenated.toString()); // only now is the string built
|
||||||
// Output: You can use the StringBuilder class.
|
// Output: You can use the StringBuilder class.
|
||||||
|
|
||||||
// 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 + " ";
|
||||||
}
|
}
|
||||||
@ -246,12 +247,12 @@ public class LearnJava {
|
|||||||
// inefficientString requires a lot more work to produce, as it generates a String on every loop iteration.
|
// inefficientString requires a lot more work to produce, as it generates a String on every loop iteration.
|
||||||
// Simple concatenation with + is compiled to a StringBuilder and toString()
|
// Simple concatenation with + is compiled to a StringBuilder and toString()
|
||||||
// Avoid string concatenation in loops.
|
// Avoid string concatenation in loops.
|
||||||
|
|
||||||
// #3 - with String formatter
|
// #3 - with String formatter
|
||||||
// Another alternative way to create strings. Fast and readable.
|
// Another alternative way to create strings. Fast and readable.
|
||||||
String.format("%s may prefer %s.", "Or you", "String.format()");
|
String.format("%s may prefer %s.", "Or you", "String.format()");
|
||||||
// Output: Or you may prefer String.format().
|
// Output: Or you may prefer String.format().
|
||||||
|
|
||||||
// Arrays
|
// Arrays
|
||||||
// The array size must be decided upon instantiation
|
// The array size must be decided upon instantiation
|
||||||
// The following formats work for declaring an array
|
// The following formats work for declaring an array
|
||||||
@ -387,7 +388,7 @@ public class LearnJava {
|
|||||||
// Iterated 10 times, fooFor 0->9
|
// Iterated 10 times, fooFor 0->9
|
||||||
}
|
}
|
||||||
System.out.println("fooFor Value: " + fooFor);
|
System.out.println("fooFor Value: " + fooFor);
|
||||||
|
|
||||||
// Nested For Loop Exit with Label
|
// Nested For Loop Exit with Label
|
||||||
outer:
|
outer:
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 10; i++) {
|
||||||
@ -398,7 +399,7 @@ public class LearnJava {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For Each Loop
|
// For Each Loop
|
||||||
// The for loop is also able to iterate over arrays as well as objects
|
// The for loop is also able to iterate over arrays as well as objects
|
||||||
// that implement the Iterable interface.
|
// that implement the Iterable interface.
|
||||||
@ -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) {
|
||||||
@ -429,38 +431,21 @@ public class LearnJava {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
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+
|
||||||
// the try-with-resources statement is also available. Try-with-resources
|
// the try-with-resources statement is also available. Try-with-resources
|
||||||
// simplifies try-catch-finally statements by closing resources
|
// simplifies try-catch-finally statements by closing resources
|
||||||
// automatically.
|
// automatically.
|
||||||
|
|
||||||
// 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
|
||||||
// an Exception.
|
// an Exception.
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
//The resource will be closed before the catch statement executes.
|
//The resource will be closed before the catch statement executes.
|
||||||
System.out.println("readLine() failed.");
|
System.out.println("readLine() failed.");
|
||||||
@ -470,8 +455,8 @@ public class LearnJava {
|
|||||||
// a finally statement might not be called.
|
// a finally statement might not be called.
|
||||||
// To learn more:
|
// To learn more:
|
||||||
// https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
|
// https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
|
||||||
|
|
||||||
|
|
||||||
// Conditional Shorthand
|
// Conditional Shorthand
|
||||||
// You can use the '?' operator for quick assignments or logic forks.
|
// You can use the '?' operator for quick assignments or logic forks.
|
||||||
// Reads as "If (statement) is true, use <first value>, otherwise, use
|
// Reads as "If (statement) is true, use <first value>, otherwise, use
|
||||||
@ -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
|
||||||
///////////////////////////////////////
|
///////////////////////////////////////
|
||||||
@ -566,10 +546,10 @@ class Bicycle {
|
|||||||
String name; // default: Only accessible from within this package
|
String name; // default: Only accessible from within this package
|
||||||
static String className; // Static class variable
|
static String className; // Static class variable
|
||||||
|
|
||||||
// Static block
|
// Static block
|
||||||
// Java has no implementation of static constructors, but
|
// Java has no implementation of static constructors, but
|
||||||
// has a static block that can be used to initialize class variables
|
// has a static block that can be used to initialize class variables
|
||||||
// (static variables).
|
// (static variables).
|
||||||
// This block will be called when the class is loaded.
|
// This block will be called when the class is loaded.
|
||||||
static {
|
static {
|
||||||
className = "Bicycle";
|
className = "Bicycle";
|
||||||
@ -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,
|
||||||
@ -734,7 +723,7 @@ public abstract class Animal
|
|||||||
|
|
||||||
public void printAge()
|
public void printAge()
|
||||||
{
|
{
|
||||||
System.out.println(age);
|
System.out.println(age);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Abstract classes can have main function.
|
// Abstract classes can have main function.
|
||||||
@ -816,18 +805,18 @@ public abstract class Mammal()
|
|||||||
// you would specify a days-of-the-week enum type as:
|
// you would specify a days-of-the-week enum type as:
|
||||||
public enum Day {
|
public enum Day {
|
||||||
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
|
||||||
THURSDAY, FRIDAY, SATURDAY
|
THURSDAY, FRIDAY, SATURDAY
|
||||||
}
|
}
|
||||||
|
|
||||||
// We can use our enum Day like that:
|
// We can use our enum Day like that:
|
||||||
public class EnumTest {
|
public class EnumTest {
|
||||||
// Variable Enum
|
// Variable Enum
|
||||||
Day day;
|
Day day;
|
||||||
|
|
||||||
public EnumTest(Day day) {
|
public EnumTest(Day day) {
|
||||||
this.day = day;
|
this.day = day;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void tellItLikeItIs() {
|
public void tellItLikeItIs() {
|
||||||
switch (day) {
|
switch (day) {
|
||||||
case MONDAY:
|
case MONDAY:
|
||||||
@ -835,17 +824,17 @@ public class EnumTest {
|
|||||||
break;
|
break;
|
||||||
case FRIDAY:
|
case FRIDAY:
|
||||||
System.out.println("Fridays are better.");
|
System.out.println("Fridays are better.");
|
||||||
break;
|
break;
|
||||||
case SATURDAY:
|
case SATURDAY:
|
||||||
case SUNDAY:
|
case SUNDAY:
|
||||||
System.out.println("Weekends are best.");
|
System.out.println("Weekends are best.");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
System.out.println("Midweek days are so-so.");
|
System.out.println("Midweek days are so-so.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
EnumTest firstDay = new EnumTest(Day.MONDAY);
|
EnumTest firstDay = new EnumTest(Day.MONDAY);
|
||||||
firstDay.tellItLikeItIs(); // => Mondays are bad.
|
firstDay.tellItLikeItIs(); // => Mondays are bad.
|
||||||
@ -854,7 +843,7 @@ public class EnumTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enum types are much more powerful than we show above.
|
// Enum types are much more powerful than we show above.
|
||||||
// The enum body can include methods and other fields.
|
// The enum body can include methods and other fields.
|
||||||
// You can see more at https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
|
// You can see more at https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user