java basics--data types, basic program structure

Keywords: Java

JAVA Basic Concepts

Program example

//public access modifiers that control the level of access other parts of the program have to this code
// The keyword class indicates that all content in a Java program is contained in a class, where you only need to use the class as a container for loader logic
//Program logic defines the behavior of a program
//All content in java application Chen Xu must be placed in classes
//The System.out.println() method wraps lines automatically when used, and System.out has a print method that does not wrap lines.

public class FirstSample
    public static void main(String[] args)
        System.out.println("Hellow World\n");

java data type


type Storage requirements Range of values
int 4 Bytes -2147483648~2147483647
short 2 bytes -32768~32767
long 8 bytes -9223372036854775808~9223372036854775807
byte 1 byte -128~127

Floating Point Type

Floating-point types are used to represent numeric values with fractional parts, and there are two types of floating-point in java

type Storage requirements Range of values
float 4 Bytes Approximately +3.40282347E+38F (precision 6-7 bits)
double 8 bytes Approximately +1.79769313486231570E+308 (precision 15 bits)

Most data types default to double because float s are difficult to satisfy in many cases

Float values of type float have a suffix F or F (for example, 3.14f), and floating-point values without a suffix F, such as 3.14, default to type double, although you can also add D or d after floating-point numbers if you want.

char type

Character, one or two bytes

bool type

The bool type has two values: false and true, which are used for logical judgment and cannot be converted between integer bools.


Using final in java is just a constant

In java, you often want a constant to be used in more than one method in a class. Usually these constants become class constants. You can set a class constant using static final. If a class vector is declared public, then methods in other classes can also use this constant.

String type

Strings in java are not like character arrays in C. Strings in java are like char* pointers, char * greeting = "Hello"

// Convert a string to an array
int[] codePoints = str.codePoints().toArray();
// Convert an array to a string
String str = new String(codePoints, 0, codePoints.length);

== is overloaded in C++ and can be used for string judgment, but it should never be used in Java. When judging string equality in java, function must be used for judgment.

string API

char charAt(int index)
// Returns the code unit at a given location and does not need to call this method unless you are interested in the underlying code unit
int codePointAt(int index)
// Returns the code point at a given location
int offsetByCodePoints(int startIndex, int cpCount)
//Returns the code point index after shifting the cpCount starting from the startIndex code point


Input and Output of Console in java

Output of a string and a number in Java is well known, and the System.out.println() method is fine, but it is relatively cumbersome to enter in java.

To get input from the console, you first need to define a Scanner object and bind it to

 *  @param Contains commonly used tool functions in java 
import java.util.*;

public class inputtest
    public static void main(String[] args)
        Scanner in = new Scanner(;

        // get first input
        System.out.println("What is your name? ");
        String name = in.nextLine();

        //get scond input
        System.out.println("How old are you ? ");
        int age = in.nextInt();

        System.out.println("Hello, " + name + ". Next year, you will be " + (age + 1));

Loops in java

import java.util.*;

// this code display while
public class retirement
    public static void main(String[] args) 
        Scanner in = new Scanner(;
        System.out.print("How much money do you need to rerire ?");

        double goal = in.nextDouble();
        System.out.print("How much money will you contribute every year?");

        double payment = in.nextDouble();

        System.out.print("Interface rate in %: ");
        double interestRate = in.nextDouble();
        double balence = 0;
        int years = 0;
        while(balence < goal)
            balence += payment;
            double interest = balence * interestRate / 100;
            balence += interest;

        System.out.println("You can retire in "+years + "years.");

do-while Loop

import java.util.*;

public class Retirement2
   public static void main(String[] args)
       	//Create a Scanner object to read the input stream
      Scanner in = new Scanner(;

      System.out.print("How much money will you contribute every year? ");
      double payment = in.nextDouble();

      System.out.print("Interest rate in %: ");
      double interestRate = in.nextDouble();

      double balance = 0;
      int year = 0;

      String input;

      // update account balance while user isn't ready to retire
         // add this year's payment and interest
         balance += payment;
         double interest = balance * interestRate / 100;
         balance += interest;


         // print current balance
         System.out.printf("After year %d, your balance is %,.2f%n", year, balance);

         // ask if ready to retire and get input
         System.out.print("Ready to retire? (Y/N) ");
         input =;
      while (input.equals("N"));


368 original articles published, 148 awarded, 330,000 visits+
His message board follow

Posted by FRSH on Thu, 12 Mar 2020 18:10:35 -0700