20145207 the third week of Java Program Design Study summary

Source: Internet
Author: User
Tags array length shallow copy

Summary of learning contents of textbook

This part may have to pick up some textbooks and things. In chapter Three, we know that Java can be differentiated into two types of systems, the basic type and the class type, where the class type is also referred to as the reference type. In this week, I learned about class types. Object: A specific entity that exists that has an explicit state and behavior class (Class): A set of objects that have the same properties and behavior, a mechanism for combining operations and properties that are common to each object. In a nutshell, a class is a design diagram of an object that is an instance of a class.

To profoundly understand the meaning of an object, such as Clothes C1 = new Clothes () C1 is referenced on the stack, new Clothes () builds the object on the heap

Java adheres to the IEEE754 floating-point arithmetic specification, using fractions and exponents to represent floating-point numbers, which can result in inaccurate representations, resulting in computational errors, such as 1.0-0.8, which is not 0.2. Therefore, if precision is required, it is necessary to use the floating-point number carefully, not directly compare the results of floating-point numbers with = =, so you can use the Java.math.BigDecimal class for better accuracy.

Basic Type Packager

Packaging Basic types: Primitive type long, int, double, float, Boolean, etc., before j2se5.0 must be packed into objects such as long, Integer, double, float, Boolean, etc., before you can manipulate them as objects.

== with the equals the Difference equals () is logically equal, compared to whether the actual character content of the string is the same, with equals () = = Whether the 2 objects produced by this class are the same object, and if it is the same object, then the return value of = = is true, otherwise, false. In Java in order to consider efficiency, "" in the exact same string, no matter how many times in the program code, the JVM will create a string instance placed in the strings pool

Array Object

An array is an object in Java, and the index starts with 0, and if access exceeds the index range,

int []arr = new int[2];

System.out.println (Arr[3]);

The arrayindexoutofboundsexception array is thrown out of bounds exception error

Int[] arr =null;

System.out.println (Arr[0]);

An arr reference does not point to an entity, but when an element in an entity is manipulated, an error that nullpointerexception a null pointer exception is thrown

public class XY

{

public static void Main (string[] args)

{

Int[][] cords={

{A-i},

{4,5,6}

};

For (int[] row:cords)

{

for (int value:row)

{

System.out.printf ("%2d", value);

}

System.out.println ();

}

}

}

Declares a two-dimensional array to store the values to be placed in the XY coordinate position. It's easy to understand a little bit of a two-dimensional array as a matrix. Format 1: element type [] Array name =new element type [element number or array length]; for example int[] arr = new INT[5]; Format 2: element type [] Array name = new element type [] {element, element ...} int[] arr= new int[]{3,5,1,7}; Int[] arr= {3,5,1,7};

stack memory : Used to store local variables, when the data is used up, the occupied space will be placed in the heap memory. heap Memory : Arrays and objects, all new instances are placed in heap memory each entity has a default initialization value for the variables in the memory address value entity

Whether System.arraycopy () or arrays.copyof () is used in an array declared by a class type, a shallow copy is performed. If you want to make a deep copy

Class Clothes2

{

String color;

char size;

Clothes2 (String color, char size)

{

This.color=color;

This.size=size;

}

}

public class Deepcopy

{

public static void Main (string[] args)

{

Clothes2[] C1 = {new Clothes2 ("Red", ' S '), New Clothes2 ("green", ' M ')};

clothes2[] C2 = new Clothes2[c1.length];

for (int i = 0; i < c1.length; i++)

{

Clothes2 C = new Clothes2 (C1[i].color, c1[i].size);

C2[i] = c;

}

C1[0].color = "Yellow";

System.out.println (C2[0].color);

}

}

You need to copy the elements yourself, copying each element in the object from C1 to C2.

String Object

The string essence is an object that packs a character array and is an instance of the Java.lang.String class that is immutable: in Java, once a string object is established, it cannot change anything in the object, and there is no way for the object to change the contents of the string. You can use the + connection string instead of StringBuilder to improve. It is characterized by the return of the original StringBuilder object after each call, thus avoiding the creation of multiple objects.

The 1+2+...+100 can be used for

public class sum1{

public static void Main (string[] args) {

for (int i=1; i<101; i++) {

System.out.print (i);

if (i!=100) {

System.oyt.print (' + ');

}

}

Improve with StringBuilder

public class OneTo100

{

public static void Main (string[] args)

{

StringBuilder builder = new StringBuilder ();

for (int i = 1; i <; i++)

{

Builder.append (i). Append (' + ');

}

System.out.println (Builder.append (+). toString ());

}

}

Produces only one StringBuilder object, one output at a time, which works better.

Enquiry Java API file

If we want to use a certain function of a class, but do not know how to invoke it, or want to know what kind of function a particular class has, we need to query the Java API file. In the Windows system, you can download the CHM format Java API, and then the specific process can refer to the textbook, offline query is very convenient.

Object Encapsulation

Encapsulation : Refers to the properties and implementation details of hidden objects, providing public access only to the outside. Encapsulation principle: Hide the content that does not need to be provided externally, the properties are hidden, providing public methods to access it.

Private ( privately ) The keyword is a permission modifier that is used to decorate members (member variables and member functions), and the privatized members are only valid in this class.

The constructor function name is the same as the class name. There is no need to declare a return type. When an object is created, the data member is initialized and, if there is no initial specified value, it is initialized with a default value. You cannot write a return statement. Function: initializes the object.

Use This The keyword is used when the object that invokes the function is needed within the function. This represents a reference to the Caprice in which the function belongs. For example: this.number= number;

Static ( statically ) The keyword static keyword is used to decorate members (member variables and member functions). Be aware that static methods can only access static members, and static methods cannot write This,super keywords, and the main function must be static. You can use the import static syntax after JDK5, and you can make fewer words when using static members.

Import Java.util.Scanner;

Import static java.lang.System.in;

Import static java.lang.System.out;

public class Importstatic

{

public static void Main (string[] args)

{

Scanner Scanner = new Scanner (in);

Out.print ("Please enter Name:");

out.printf ("%s Hello!") %n ", Scanner.nextline ());

}

}

Problems in learning and the process of solving them

Code in the book

Integer i = 10;

System.out.println (I+10);

System.out.println (i++);

Think it will show 20 and 11, correct should be 20 and 10. The book here is wrong. 10 will be boxed first, and then in the i+10, I will be the first unpacking, and then the addition operation; i++ the row is also the first to remove the box and then increment operation. Increment is the last operation and is incremented only after the output.

Problems in code debugging and the resolution process

Import Java.util.Scanner;

public class Guess

{

public static void Main (string[] args)

{

Scanner Scanner = new Scanner (system.in);

int number = (int) (Math.random () * 10);

int guess;

Do

{

System.out.printf ("GUESS A Number:");

Guess = Scanner.nextint ();

}

while (guess! = number);

System.out.println ("You are right!");

}

}

Use import Java.util.Scanner to tell the compiler to lazy if there is no compile error, unless you enter java.util.Scanner each time. Use system.in input when creating an scanner instance. At first, some of the code functions are not understood, and the Math.random () is to make the system randomly select a pseudo-random double value greater than or equal to 0.0 and less than 1.0, *10 and cast to int, which can generate a random integer of 1~10. And guess = Scanner.nextint (); Ask for this integer to be guessed until the right is right! ”

20145207 the third week of Java Program Design Study summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.