JAVA basic concepts
Program Example
//public 访问修饰符,这些修饰符用于控制程序的其他部分对这段代码的访问级别
// 关键字 class 表明Java程序中全部的内容都包含在类中,这里只需要将类作为加载程序逻辑的容器
//程序逻辑定义了程序的行为
//java应用陈旭中的全部内容都必须放置在类中
//System.out.println() 方法使用会进行自动换行,System.out还有一个print方法,该方法不会进行换行
public class FirstSample
{
public static void main(String[] args)
{
System.out.println("Hellow World\n");
}
}
java data types
Integer
Types of | Storage requirements | Ranges |
---|---|---|
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 type is used to represent the fractional part of the numerical value, there are two floating point types in java
Types of | Storage requirements | Ranges |
---|---|---|
float | 4 bytes | About ± 3.40282347E + 38F (6-7 bit accuracy) |
double | 8 bytes | About ± 1.79769313486231570E + 308 (precision 15) |
Most of the data type defaults to double, float because in many cases it is difficult to meet the demand
Has a value of type float, or F suffix f (e.g. 3.14f), no floating point value, such as 3.14 F suffix default type double type, of course, if you are willing to add floating-point D or d later.
char type
Characters, a byte or two bytes
bool type
bool
There are two types of values: false
and true
for logical judgment, good integer bool
not be phase conversion between
constant
java
Used final
to only constants
In java often desirable constant using a plurality of methods in a class, the classes typically these constants become constants, can be provided using static final classes a constant, if a class vector is then declared as public methods in other classes also be able to use this constant.
String type
Not as a string in java array of characters in C language like java strings like char * pointer, like,char * greeting = "Hello"
// 将一个字符串转换为数组
int[] codePoints = str.codePoints().toArray();
// 将一个数组转换为字符串
String str = new String(codePoints, 0, codePoints.length);
In C ++ on ==
is overloaded, allows the determination of the string, but this must not be used in Java, java string must be carried out using the equivalence determination function determines that
string API
char charAt(int index)
// 返回给定位置的代码单元,除非对底层的代码单元感兴趣,否则不需要调用这个方法
int codePointAt(int index)
// 返回给定位置的码点
int offsetByCodePoints(int startIndex, int cpCount)
//返回从startIndex代码点开始,位移cpCount后的码点索引
...
java console input and output
java digital output and a string to use as we all know, System.out.println () method can be, but input to relatively little trouble in java.
To get input from the console, you first need to define a Scanner object and the object and bind System.in
inputtest.java
/**
* @param 包含java中常用的工具函数
*/
import java.util.*;
public class inputtest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
// 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));
}
}
java in circulation
import java.util.*;
// this code display while
public class retirement
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
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;
years++;
}
System.out.println("You can retire in "+years + "years.");
}
}
do-while loop
import java.util.*;
public class Retirement2
{
public static void main(String[] args)
{
//创建一个 Scanner对象,用于读取输入流
Scanner in = new Scanner(System.in);
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
do
{
// add this year's payment and interest
balance += payment;
double interest = balance * interestRate / 100;
balance += interest;
year++;
// 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 = in.next();
}
while (input.equals("N"));
}
}
71