JAVA基础概念
程序示例
//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数据类型
整型
类型 | 存储需求 | 取值范围 |
---|---|---|
int | 4字节 | -2147483648~2147483647 |
short | 2字节 | -32768~32767 |
long | 8字节 | -9223372036854775808~9223372036854775807 |
byte | 1字节 | -128~127 |
浮点类型
浮点类型用于表示有小数部分的数值,在java中有两种浮点类型
类型 | 存储需求 | 取值范围 |
---|---|---|
float | 4字节 | 大约±3.40282347E+38F(精度6-7位) |
double | 8字节 | 大约±1.79769313486231570E+308(精度15位) |
大部分的数据类型默认为double,因为float在很多情况下很难满足需求
float类型的数值有一个后缀F或者f(例如3.14f),没有后缀F的浮点数值如3.14默认是 double类型的类型,当然如果你愿意也可以在浮点数后面添加D或者d。
char类型
字符,一个字节或两个字节
bool类型
bool
类型有两个值:false
和true
,用来进行逻辑判断,整型好bool
之间不能进行相转换
常量
java
中使用final
来只是常量
在java中经常希望一个常量可以在一个类中多个方法中使用,通常将这些常量成为类常量,可以使用static final设置一个类常量,如果一个类向量被声明为public那么其他类中的方法也能够使用这个常量。
字符串类型
java中的字符串并不像C语言中的字符数组一样,java中的字符串类似于char*指针一样,char * greeting = "Hello"
// 将一个字符串转换为数组
int[] codePoints = str.codePoints().toArray();
// 将一个数组转换为字符串
String str = new String(codePoints, 0, codePoints.length);
C++中对==
进行了重载,可以用于字符串的判断,但是在Java中绝对不能这样用,java中进行字符串相等判断时一定要使用函数进行判断
string API
char charAt(int index)
// 返回给定位置的代码单元,除非对底层的代码单元感兴趣,否则不需要调用这个方法
int codePointAt(int index)
// 返回给定位置的码点
int offsetByCodePoints(int startIndex, int cpCount)
//返回从startIndex代码点开始,位移cpCount后的码点索引
...
java中控制台的输入输出
java中输出一个字符串和数字大家都知道使用, System.out.println()方法即可,但是在java中进行输入要相对来说麻烦一点。
要想从控制台获取输入,首先需要定义一个Scanner对象,并将该对象与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中的循环
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循环
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