Java basic programming structure and Java programming structure

Source: Internet
Author: User
Tags mathematical functions

Java basic programming structure and Java programming structure

Public is the access modifier)
System. out. println (); // wrap automatically after output
System. out. print (); // No line break after output

Note:
1. add // 2 to the front of each line. /* content to be annotated */but not in the annotated code */3. /** content to be annotated */This annotation can automatically generate a document
Data Type:
1. long Integer ending with L 2. the end of float Type F. If no value is added, it is automatically considered as double type 3. calculate the square root of a negative number or the result of 0/0 is NaN (not a number) 4. floating point numbers are not applicable to financial calculations that prohibit rounding errors. Because floating point values are represented by binary systems, they cannot accurately represent 1/10. If you do not need to include any rounding error in numerical calculation, you should use the BigDecimal class. 5. boolean type: true and false, used to determine logical conditions. Integer and boolean values cannot be converted to each other.
Variable:
1. The variable must start with a letter and the variable name is case sensitive. 2. The CamelCase naming rule for variables, but the first letter must be in lower case. 3. Variables without initial values cannot be used.
Constant:
1. final double CM_PER_INCH = 2.54; // The keyword final indicates that this variable can only be assigned once. Once assigned, it cannot be changed.
Relational operators:
1. triplicate operation: conditon? Condition1: condition2 examples: x <y? X: y // return the smaller value of x and y.
Mathematical functions and constants:
For example, to calculate the square root of a value, use the sqrt Method double x = 4; double y = Math. sqrt (x); System. out. println (y); // Add import static java to the top of prints 2.0. lang. math. *; then, Math will not be added before the method name and variable name.
Forced type conversion:
For example, double x = 9.997; int nx = (int) x; // the nx value is 9. If you round the floating point, you can obtain the nearest integer, you need to use Math. round () method: double x = 9.997; int nx = (int) Math. round (x); // the nx value is 10
String:
1. substring: The substring method in the String class can extract a substring from a large String, for example, String greeting = "Hello"; String s = greeting. substring (0th); // creates a string () consisting of "El", indicating that it starts from until 3, but does not include 3, that is, the string length is 3. 2. check whether the string is equal: You can use the equals method: s. equals (t). If the character string s is the same as the string t, true is returned; otherwise, false is returned. you can also: "Hello ". equals ("greeting "). If you check whether the two strings are equal and case-insensitive, you can use the equalsIgnoreCase method: "Hello ". equalsIgnoreCase ("hello") *** Note: you cannot use the = Operator to check whether two strings are equal! ***
Code added by android3.0:
Add android. enableAapt2 = false to gradle. properties.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.