java基础 -- 数据类型,基本程序结构
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 inputSystem.out.println("What is your name? ");String name = in.nextLine();//get scond inputSystem.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 retiredo{// add this year's payment and interestbalance += payment;double interest = balance * interestRate / 100;balance += interest;year++;// print current balanceSystem.out.printf("After year %d, your balance is %,.2f%n", year, balance);// ask if ready to retire and get inputSystem.out.print("Ready to retire? (Y/N) ");input = in.next();}while (input.equals("N"));} }71
總結
以上是生活随笔為你收集整理的java基础 -- 数据类型,基本程序结构的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 作者:陈康(1976-),男,博士,清华
 - 下一篇: UML对象关系与箭头对应表