I needed to get Java Silver [^ 1], so I will summarize it after studying. [^ 1]: I used the Oracle Certification Textbook Java Programmer Silver SE 8 https://www.shoeisha.co.jp/book/detail/9784798142739
In Java, processing is always executed from the main () method.
Sample.java
public static void main(String[] args) {}
public static void main (string [] args) {}
string [] s is uppercasepublic static main (String [] args) {}
void is missingpublic static void main (String []) {}
args missingpublic static void main (String args) {}
There is no [] after the Stringpublic void main (String [] args) {}
There is no staticpublic static void Main (String [] args) {}
Main M is lowercaseThere is only one class specified as public per source file. If a class specified as public is defined, the source file name should be the same as the class name specified as public.
Test.java
class Foo {}
class Bar {}
Foo.java
public class Foo {}
class Bar
Foo.java
//There are two public classes in one source file
public class Foo {}
public class Bar {}
Bar.java
//The source file name is different from the class name specified by public
public class Foo {}
class Bar {}
The comments are to make the program easier to understand. Not subject to compilation.
//Comments up to the end of the line
/*Multiple lines of comments can be specified*/
/**Multiple lines of comments can be specified. Comments can be converted to HTML with the javadoc command. (Document comment)*/
The value or notation written in the source code. There are mainly 6 types.
A value that does not have a decimal part. It can be expressed in decimal, binary, octal, and hexadecimal.
Base number | Example | Description |
---|---|---|
Decimal number | 255 | Express using numbers from 0 to 9 |
Binary number | 0b101 | Expressed using the numbers 0 and 1 先頭に0bを入れるとBinary numberと判断される |
8 base | 0377 | Express using numbers from 0 to 7 先頭に0を入れると8 baseと判断される |
Hexadecimal | 0xff | Expressed by numbers from 0 to 9 and alphabets from a to f 先頭に0xを入れるとHexadecimalと判断される |
A value with a decimal part. Can express decimal numbers and exponents.
Base number | Example | Description |
---|---|---|
Decimal number | 12.33 | - |
index | 3e4 | 3.0 x 10 to the 4th power(30000.0) indexを表すeを使う |
Represent one character. Enclose the characters in "'" (single quotes). Escape sequences can also be represented by "" (backslash).
Example | Description | |
---|---|---|
One character | 'A' | Enclose one character in single quotes |
Unicode | '\u3012' | \Specifying a 4-digit hexadecimal number after u results in a Unicode value |
Escape sequence | meaning |
---|---|
\n | new line |
\r | return |
\t | Tab character |
\b | Backspace |
' | Single quote |
" | Double quote |
\|backslash | |
\f | Form feed |
Represents a character string that is a collection of multiple characters. Enclose the string in "" "(double quotes).
Express the boolean value as true or false.
Express the meaning of "not referring to anything" when using reference type data.
Starting with Java SE 7, you can use "_" (underscore) in the middle of numbers. It enhances the readability of numeric literals and can express "100,000" as "100 \ _000". There are the following rules for using underscores.
-Cannot be used at the beginning or end of a literal. -Cannot be used before or after the decimal point in a floating point literal. -Cannot be used before F, which represents a float value, and L, which represents a long value. -Cannot be used in the middle or before or after 0x used in hexadecimal and 0b used in binary. In summary, it cannot be used at the beginning, end, or before or after a symbol of a literal.
A box to put data in. Give the variable a name (** variable name **) to distinguish it from other boxes.
The names given to variables, classes, and methods are called identifiers. The identifier has the following rules.
・ The first character of the identifier is only alphabetic characters, dollar symbols, and underscores. ・ Numbers can be used after the second character of the identifier ・ Reserved words cannot be used ・ Case sensitive ・ There is no character limit
Reserved words are names that are already used in Java and include the following.
Reserved word(null, true, falseはReserved wordではないがリテラルとして扱われるため使用不可) |
---|
abstract, assert, boolean, break, type, byte, case |
catch, char, class, const, continue, default |
do, double, else, enum, extends, final |
finally, float, for, goto, if, implements |
import, instanceof, int, interface, long, native |
new, package, private, protected, public, return |
short, static, strictfp, super, switch, syncronized |
this, throw, throws, transient, try, void |
volatile, while, null, true, false |
Specify what kind of value to put when preparing a variable by data type. Java data types can be broadly divided into basic data types and reference types.
Basic data types: integers, characters, etc. Reference type: A type other than the basic data type including classes, arrays, interfaces, etc.
There are eight basic data types as follows.
Data type | meaning | size | Representable value |
---|---|---|---|
byte type | Signed integer | 8 bits | |
short type | Signed integer | 16 bit | |
int type | Signed integer | 32 bit | |
long type | Signed integer | 64-bit | |
float type | Floating point number | 32 bit | Value based on IEEE754 |
double type | Floating point number | 64-bit | Value based on IEEE754 |
char type | One character that can be expressed in Unicode | 16 bit | \u0000 〜 \uFFFF |
boolean type | Boolean value | 1 bit | true, false |
Variable declaration: Prepare variables
Data type variable name; //Variable declaration
Assignment: To store a value in a variable
Variable name=value; //Assign a value to a variable
Variable initialization: Assign initial values before starting to use variables Variable declarations and assignments can also be written on one line.
Data type variable name=value; //Variable declaration and assignment at the same time
To put a long value in a long type variable, add L or l to the literal. To put a float value in a float type variable, add F or f to the literal.
long l = 40L;
float f = 1.15F;
Sample.java
class Sample {
public static void main(String[] args) {
long num1 = 10000000000; //Compile error
long num2 = 10000000000L;
float num3 = 10.0; //Compile error
float num4 = 10.0F;
}
}
Signed integer literals are recognized as int by default in Java. 10000000000 on the 3rd line has no problem as a long type value, but it has a number of digits that cannot be handled as an int type, so a compile error occurs. Therefore, L is required at the end of the literal as in the 4th line. Floating-point literals are recognized as double by default. Therefore, 10.0 on the 5th line is first recognized as a double type (64 bits) and then tried to be assigned to a float type, resulting in a compile error. Therefore, F is required at the end of the literal.
Variables can handle changing values, but variables can also be prepared to handle fixed values. In Java, this variable is declared as a constant using the final modifier.
final data type constant name=initial value;
A value declared as a constant cannot be assigned to another value after initialization.
In Java, a character string, which is a collection of multiple characters, is treated as reference type data. String type is used as the data type when handling character strings. A reference type variable is also called a reference variable in the sense that it is a variable that points to a block of values.
char type//Basic data type(Only one character can be handled)
String type//Reference type(Can handle multiple characters)
The range of variables that can be used is limited by the place where they are declared. This is called ** scope (effective range) **.
Sample.java
class Sample {
public static void main(String[] args) {
{
int x = 100;
x = 200;
}
// x = 300;
}
}
In the above Sample, the 3rd to 6th lines are surrounded by {} blocks, and the variable x is declared in it. Therefore, the scope of the variable x is from the 3rd line to the 6th line, and if you uncomment the 7th line, a compile error will occur.
Recommended Posts