类型 | Bit/Bytes | 值范围 |
---|---|---|
boolean | 1 bit | True 或 False |
char | 16 bit/ 2 bytes | 0 - 65535 |
byte | 8 bit/ 1 byte | -128 - 127 |
short | 16 bit/ 2 bytes | -32768 - 32767 |
int | 32 bits/ 4 bytes | -2147483648 - 2147483647 |
long | 64 bits/ 8 bytes | -9,223,372,036,854,775,808 至 -9,223,372,036,854,775,808 (-2^63 至 -2^63) |
float | 32 bits/ 4 bytes | -3.4028235 x 10^38 至 3.4028235 x 10^38 |
double | 64 bits/ 8 bytes | -1.7976931348623157 x 10^308 至 1.7976931348623157 x 10^308 |
// Create variable a, and assign a value of 100. int a = 100; // Assign new value to a a = 200; // Create variable b, assigned equal to a int b = a;
// Declare and initialize the object. Object a = new Object(); // reinitialize object a = new String("Text"); // Declare object 'b' and assign it to the object 'a'. Object b = a;
// Create variable a, assign a value of 200 // A memory area (1) is created that contains the value of 200. int a = 200; // Create a variable b, assign a value of 200. // A memory area (2) is created that contains the value of 200. int b = 200; // The primitive compared with each other by value // return true. boolean c = (a == b);
package com.yiibai.tutorial.comparation; public class ReferenceEeDemo { public static void main(String[] args) { // NOTE: For String, two ways to initialize the object below are not the same: String str1 = "String 1"; String str2 = new String("String 1"); // The new operator create the memory (1) // Contains the "This is text" String // And s1 is a reference that points to (1) String s1 = new String("This is text"); // The new operator create the memory (2) // Contains the "This is text" String // And s2 is a reference that points to (2) String s2 = new String("This is text"); // Use the operator== to compare s1 and s2. // Results in false. // It is obviously different from what you think. // The reason is the type of reference // Operator == compares the positions that they point to. boolean e1 = (s1 == s2); // false System.out.println("s1 == s2 ? " + e1); // Without any new operator. // Java creates a reference named 'obj' // And pointing to a memory area that s1 points to. Object obj = s1; // 2 references 'obj' and 's1' are all pointing to one area of memory. // Results is true boolean e2 = (obj == s1); // true System.out.println("obj == s1 ? " + e2); } }
package com.yiibai.tutorial.comparation; public class StringComparationDemo { public static void main(String[] args) { String s1 = new String("This is text"); String s2 = new String("This is text"); // s1 and s2 Comparison, use equals(..) boolean e1 = s1.equals(s2); // true System.out.println("first comparation: s1 equals s2 ? " + e1); s2 = new String("New s2 text"); boolean e2 = s1.equals(s2); // false System.out.println("second comparation: s1 equals s2 ? " + e2); } }
first comparation: s1 equals s2 ? true second comparation: s1 equals s2 ? false
package com.yiibai.tutorial.comparation.equals; public class NumberOfMedals { // Number of gold medals private int goldCount; // Number of silver medals private int silverCount; // Number of bronze medals private int bronzeCount; public NumberOfMedals(int goldCount, int silverCount, int bronzeCount) { this.goldCount = goldCount; this.silverCount = silverCount; this.bronzeCount = bronzeCount; } public int getGoldCount() { return goldCount; } public int getSilverCount() { return silverCount; } public int getBronzeCount() { return bronzeCount; } // Override the equals(Object) method of class Object. @Override public boolean equals(Object other) { // if other is null, return false. if (other == null) { return false; } // If the 'other' is not a NumberOfMedals // return false if (!(other instanceof NumberOfMedals)) { return false; } NumberOfMedals otherNoM = (NumberOfMedals) other; if (this.goldCount == otherNoM.goldCount && this.silverCount == otherNoM.silverCount && this.bronzeCount == otherNoM.bronzeCount) { return true; } return false; } }
package com.yiibai.tutorial.comparation.equals; public class NumberOfMedalsComparationDemo { public static void main(String[] args) { NumberOfMedals american = new NumberOfMedals(40, 15, 15); NumberOfMedals japan = new NumberOfMedals(10, 5, 20); NumberOfMedals korea = new NumberOfMedals(10, 5, 20); System.out.println("Medals of American equals Japan ? " + american.equals(japan)); System.out.println("Medals of Korea equals Japan ? " + korea.equals(japan)); } }
Medals of American equals Japan ? false Medals of Korea equals Japan ? true
package com.yiibai.tutorial.sorting; import java.util.Arrays; public class StringArraySortingDemo { public static void main(String[] args) { String[] fruits = new String[] { "Pineapple", "Apple", "Orange", "Banana" }; // Use a static method of class Arrays to sort. // Arrays.sort (Object []) Arrays.sort(fruits); for (int i = 0; i < fruits.length; i++) { System.out.println("fruits " + i + " : " + fruits[i]); } } }
fruits 0 : Apple fruits 1 : Banana fruits 2 : Orange fruits 3 : Pineapple
// You write a class which simulates an actor (Actor). // You want to arrange the order of actors according to the principle of last name prior then to first name. // At this point, it arises situations requiring to compare Actor objects. public class Actor { private String firstName; private String lastName; }
package com.yiibai.tutorial.sorting; // To be compared with each other, Actor classes // must implement Comparable interface public class Actor implements Comparable{ private String firstName; private String lastName; public Actor(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } // Compare this Actor with another Actor. // According to comparison principle of lastName first, // then compare firstName. @Override public int compareTo(Actor other) { // String can be compared with each other // (String implement Comparable interface) // Compare lastName between two objects. int value = this.lastName.compareTo(other.lastName); // If lastName of two objects are not equal. if (value != 0) { return value; } // In case lastName of 2 object are same. // Next, compare firstName. value = this.firstName.compareTo(other.firstName); return value; } }
package com.yiibai.tutorial.sorting; public class ActorSortingDemo { public static void main(String[] args) { Actor actor1 = new Actor("Mischa", "Barton"); Actor actor2 = new Actor("Christian", "Bale"); Actor actor3 = new Actor("Joan", "Collins"); Actor actor4 = new Actor("Gemma", "Arterton"); Actor actor5 = new Actor("Daniel", "Craig"); Actor[] actors = new Actor[] { actor1, actor2, actor3, actor4, actor5 }; // We use an algorithm to rearrange the array above. // Sort the Actor objects in gradually increase order. for (int i = 0; i < actors.length; i++) { for (int j = i + 1; j < actors.length; j++) { // If later Actor is smaller than prior Actor // Make the swap positions with each other. if (actors[j].compareTo(actors[i]) < 0) { // Use a temporary variable. Actor temp = actors[j]; actors[j] = actors[i]; actors[i] = temp; } } } // Print out for (int i = 0; i < actors.length; i++) { System.out.println(actors[i].getFirstName() + " " + actors[i].getLastName()); } } }
Gemma Arterton Christian Bale Mischa Barton Joan Collins Daniel Craig
package com.yiibai.tutorial.sorting; import java.util.Arrays; public class ActorSortingDemo2 { public static void main(String[] args) { Actor actor1 = new Actor("Mischa", "Barton"); Actor actor2 = new Actor("Christian", "Bale"); Actor actor3 = new Actor("Joan", "Collins"); Actor actor4 = new Actor("Gemma", "Arterton"); Actor actor5 = new Actor("Daniel", "Craig"); Actor[] actors = new Actor[] { actor1, actor2, actor3, actor4, actor5 }; // Use Arrays.sort(Object []) to arrange. Arrays.sort(actors); // Print out for (int i = 0; i < actors.length; i++) { System.out.println(actors[i].getFirstName() + " " + actors[i].getLastName()); } } }
package com.yiibai.tutorial.sorting; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class ListSortingDemo { public static void main(String[] args) { Actor actor1 = new Actor("Mischa", "Barton"); Actor actor2 = new Actor("Christian", "Bale"); Actor actor3 = new Actor("Joan", "Collins"); Actor actor4 = new Actor("Gemma", "Arterton"); Actor actor5 = new Actor("Daniel", "Craig"); // A list containing the elements can be compared with each other. Listactors = new ArrayList (); actors.add(actor1); actors.add(actor2); actors.add(actor3); actors.add(actor4); actors.add(actor5); // Use Collections.sort(List) // to sort a list. Collections.sort(actors); for (Actor actor : actors) { System.out.println(actor.getFirstName() + " " + actor.getLastName()); } } }
package com.yiibai.tutorial.comparator; public class Person { private int age; private String fullName; public Person(String fullName, int age) { this.fullName = fullName; this.age = age; } public int getAge() { return age; } public String getFullName() { return fullName; } }
package com.yiibai.tutorial.comparator; import java.util.Comparator; //This class implements the Comparatorinterface //It can be considered as the rule to compare the Person objects. public class PersonComparator implements Comparator { // Override compare methods. // Specify the comparison rules between 2 Person objects. @Override public int compare(Person o1, Person o2) { // Two null objects, regarded as equal. if (o1 == null && o2 == null) { return 0; } // If o1 null, regarded as o2 is larger if (o1 == null) { return -1; } // If o2 null, regarded as o1 is larger . if (o2 == null) { return 1; } // Rule: // Sort gradually increase over age. int value = o1.getAge() - o2.getAge(); if (value != 0) { return value; } // Case where the objects are the same age, compare to FULLNAME // Compare by Alphabet. value = o1.getFullName().compareTo(o2.getFullName()); return value; } }
package com.yiibai.tutorial.comparator; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class ComparatorSortingDemo { public static void main(String[] args) { Person person1 = new Person("Marry", 20); Person person2 = new Person("Tom", 21); Person person3 = new Person("Daniel", 21); Person person4 = new Person("Mischa", 18); Person person5 = new Person("Christian", 20); // An array is not sorted Person[] array = new Person[] { person1, person2, person3, person4, person5 }; // Sort the array, use:Arrays.sort(T[],Comparator supers T>) // Provide a Comparator. Arrays.sort(array, new PersonComparator()); for (Person person : array) { System.out.println("Person: " + person.getAge() + " / " + person.getFullName()); } System.out.println("------------------------"); // For List List list = new ArrayList (); list.add(person1); list.add(person2); list.add(person3); list.add(person4); list.add(person5); // Sort the array, use: Arrays.sort(T[],Comparator supers T>) // Provide a Comparator. Collections.sort(list, new PersonComparator()); for (Person person : list) { System.out.println("Person: " + person.getAge() + " / " + person.getFullName()); } } }
Person: 18 / Mischa Person: 20 / Christian Person: 20 / Marry Person: 21 / Daniel Person: 21 / Tom ------------------------ Person: 18 / Mischa Person: 20 / Christian Person: 20 / Marry Person: 21 / Daniel Person: 21 / Tom