Going back to the beginning and getting started with Java ① Data types and access modifiers

At the beginning

This article is an article for me to go back to the beginning and study Java again. I will post it in several parts. I hope you can see it if you are free.

environment: jdk:JavaSE8 Editor: VS Code

What to handle this time

-Frequently used Java data types -Access modifier

Frequently used Java data types

The following are the ones I often use. I will explain each of them.

Literal.java




public class Literal {

    public byte byteValue = 1;
    protected short shortValue = 1;
    int intValue = 1;
    private long longValue = 1L;
    public float floatValue = 1.0F;
    public double doubleValue = 1.0;
    public char charValue = '1';
    public boolean booleanValue = true;
    public String strValue = "1";
}

Roughly, the data is handled in the following categories.

Model name Data to handle
byte、short、int、long integer
float 、double Decimal
char Unicode
String String

In addition, these data types are primitive types (basic data types) other than String. String is called a reference type, and each ** Primitive type: Holds the value itself ** ** Reference type: Holds where the value is ** There is a difference in method arguments and processing results. ** Also, all data types other than the above except String are treated as reference types. ** **

Access modifier

Of the above source code ・ Public ・ Protected ・ Default (nothing is attached) ・ Private Is applicable.

By attaching it, you can control the access from other classes to the variable (method).

Access modifier Access range
public Accessable from anywhere
protected It can be accessed if it is a subclass of the class that defines the variable declared protected. It can be accessed regardless of inheritance if it is in the same package.
default Only accessible within the same package
private Only accessible from inside the class that defines the variable declared private

The access level becomes stricter from top to bottom. By using this rule Add private to prohibit direct rewriting of variables in the class, Allow access to variables only by manipulating public methods You can use a technique called ** encapsulation **.

Literal.java



public class Literal {
    public byte byteValue = 1;
    protected short shortValue = 1;
    int intValue = 1;
    private long longValue = 1L;
}

class TestLiteral {
    public static void main(String[] args) {
        Literal l = new Literal();
        System.out.println(l.byteValue); //Accessable because it is public
        System.out.println(l.intValue);//Accessable because it is the same package
        System.out.println(l.shortValue);//Accessable because it is the same package
        System.out.println(l.longValue);//Compile error, inaccessible from other classes
    }
}

At the end

We will continue to improve the quality and update the content of the articles as appropriate. Thank you.

※Source code https://github.com/mamoru12150927/JavaQitta.git

Recommended Posts

Going back to the beginning and getting started with Java ① Data types and access modifiers
Returning to the beginning, getting started with Java ② Control statements, loop statements
Getting started with Kotlin to send to Java developers
Getting Started with Doma-Introduction to the Criteria API
Android development-WEB access (POST) Try to communicate with the outside and send data. ~
Getting Started with Java Collection
Getting Started with Java Basics
Getting started with Java and creating an AsciiDoc editor with JavaFX
Getting Started with Reactive Streams and the JDK 9 Flow API
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
I want to return to the previous screen with kotlin and java!
Basic data types and reference types (Java)
Getting started with Java lambda expressions
Java basic data types and reference types
Read the data of Shizuoka point cloud DB with Java and try to detect the tree height.
Android development-WEB access (GET) Try to get data by communicating with the outside. ~
Getting Started with Micronaut 2.x ~ Native Build and Deploy to AWS Lambda ~
Getting Started with Ruby for Java Engineers
Getting Started with Java Starting from 0 Part 1
Getting started with the JVM's GC mechanism
[Introduction to Java] Variable declarations and types
Getting Started with Java_Chapter 8_About Instances and Classes
Links & memos for getting started with Java (for myself)
About Java data types (especially primitive types) and literals
Getting Started with Doma-Using Projection with the Criteira API
Try to link Ruby and Java with Dapr
Getting Started with Doma-Using Subqueries with the Criteria API
Getting Started with Java 1 Putting together similar things
Getting Started with Doma-Using Joins with the Criteira API
How to access Java Private methods and fields
A memo about the types of Java O/R mappers and how to select them
Form and process file and String data at the same time with Spring Boot + Java
Getting started with Java programs using Visual Studio Code
[Java] Types of comments and how to write them
I tried to get started with Spring Data JPA
[Java] Refer to and set private variables with reflection
Java classes and instances to understand in the figure
HTTPS connection with Java to the self-signed certificate server
About Java basic data types and reference type memory
I summarized the types and basics of Java exceptions
I want to transition screens with kotlin and java!
Prepare the environment for java11 and javaFx with Ubuntu 18.4
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Get started with serverless Java with the lightweight framework Micronaut!
In Java, I want to trim multiple specified characters from only the beginning and end.
Access Web API on Android with Get and process Json (Java for the time being)
What event sites do Java users often see? I tried to visualize the data collected by connpass and Doorkeeper with Reveal.