Study Java Silver 1

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

main () method

In Java, processing is always executed from the main () method.

Sample.java


public static void main(String[] args) {}
NG pattern (compile error)
  1. public static void main (string [] args) {} string [] s is uppercase
  2. public static main (String [] args) {} void is missing
  3. public static void main (String []) {} args missing
NG pattern (runtime error)
  1. public static void main (String args) {} There is no [] after the String
  2. public void main (String [] args) {} There is no static
  3. public static void Main (String [] args) {} Main M is lowercase

Source and class files

There 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.

OK pattern

Test.java


class Foo {}
class Bar {}

Foo.java


public class Foo {}
class Bar
NG pattern

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 {}

comment

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)*/

literal

The value or notation written in the source code. There are mainly 6 types.

1. Integer literal

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と判断される
2. Floating point literals

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を使う
3. Character literal

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
4. String literal

Represents a character string that is a collection of multiple characters. Enclose the string in "" "(double quotes).

5. Logical value literal

Express the boolean value as true or false.

6. null literal

Express the meaning of "not referring to anything" when using reference type data.

Numeric literals with underscores

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.

Declaration and initialization of variables and constants

variable

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
Data type

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 -2^72^7 - 1 (-128 - 127)
short type Signed integer 16 bit -2^{15}2^{15} - 1 (-32768 - 32767)
int type Signed integer 32 bit -2^{31}-2^{31} - 1
long type Signed integer 64-bit -2^{63}-2^{63} - 1
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 and assignment

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;
Default types for signed integers and floating point numbers

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.

constant

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.

The string is a reference type

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)

Variable scope (scope)

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

Study Java Silver 1
Java Silver Study Day 1
How to study Java Silver SE 8
Let's study Java
Java Silver memo
Summary of [Java silver study] package
Java 8 study (repeatable)
Java study memorandum
Passed Java SE8 Silver
java bronze silver passed
Java Silver passing experience
[Java ~ Method ~] Study memo (5)
[Java Silver] About initialization
Java study # 1 (typical type)
About inheritance (Java Silver)
[Java ~ Array ~] Study memo 4
My Study Note (Java)
Java8 Silver exam memorandum
Study Java # 2 (\ mark and operator)
Study java arrays, lists, maps
[Experience] Passed Java SE 8 Silver
JAVA Silver qualification exam record
[Java ~ Boolean value ~] Study memo (2)
Java Silver exam preparation memo
[Java Silver] About equals method
Java study # 7 (branch syntax type)
Java
Java SE8 Silver Pass Experience
Java study memo 2 with Progate
Java
Study Java with Progate Note 1
[Java Silver] Array generation method
[Note] Java Silver SE8 qualification acquired
[In-house study session] Java exception handling (2017/04/26)
Get Java Silver on your commute!
Road to Java SE 11 Silver acquisition
[Study session memo] Java Day Tokyo 2017
Java Silver Repo (Failure & Pass Experience)
Java study # 4 (conditional branching / if statement)
How to pass Oracle Java Silver
Orcacla Java Bronze SE 7/8 Qualification Study
What I learned with Java Silver
Diary for Java SE 8 Silver qualification
Java study # 5 (iteration and infinite loop)
[Qualification] Java Silver SE11 Passing Experience
Study Deep Learning from scratch in Java.
Studying Java ―― 3
[Java] array
[Java Silver] Summary of access modifier points
Summary of in-house newcomer study session [Java]
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array