目錄
- 1.final關鍵字
- 2.布爾型別
- 3.取余運算子
- 4.foreach回圈
- 5. & 和 &&,| 和 || 的關系
- 6.一位陣列
- 7.亂數
- 8.二維陣列
- 9.null的使用
- 10.物件的比較( "=="運算子 和 equals()方法的區別 )
- 11.構造方法
- 12.代碼塊
- 1)普通代碼塊
- 2)構造代碼塊
- 3)靜態代碼塊
1.final關鍵字
在java語言中主要用關鍵字final來定義常量,
必須要在常量宣告時對其進行初始化
final關鍵字可以修飾基本資料型別,物件應用和方法
被final修飾后無法再對變數進行修改
//@Description: Java中定義常量
public class TestFinal
{
static final int YEAR = 365; //一個靜態常量,常量用大寫字母表示
public static void main(String[] args)
{
System.out.println("兩年是: " + 2 * YEAR + "天");
}
}
//上面代碼的結果如下
兩年是: 730天
2.布爾型別
在Java中用關鍵字boolean來宣告布爾型別
被宣告為布爾型別的變數,只能用true和false來進行賦值
不能用1和0分別代表true和false
布爾型別不可能與其他任何資料型別進行轉換
public class booleanDemo
{
public static void main(String[] args)
{
boolean Zhang = true;
System.out.println("Zhang is man? = " + Zhang);
}
}
//上面代碼的結果如下
Zhang is man? = true
3.取余運算子
5 % 3 = 2
5 % -3 = 2
5.2 % 3.1 = 2.1
3 % 5 = 3
4.foreach回圈
雖然說得是foreach回圈但用的時候用的還是for關鍵字,只是為了與for回圈區分開來而已
public class foreachDemo
{
public static void main(String[] args)
{
int[] numArray = {1,2,3,4,5,6};
for(int i = 0;i < numArray.length;i++){
System.out.print(numArray[i] + " ");
}
System.out.println();//換行
System.out.println();//換行
for(int element : numArray)
{
//element相當于自增下標的numArray[0],numArray[1]....
System.out.print(element + " ");
}
System.out.println();
}
}
//結果如下
1 2 3 4 5 6
1 2 3 4 5 6
5. & 和 &&,| 和 || 的關系
對于與操作,有一個條件不滿足結果就是false
&
普通與(&)是,所有的判斷條件都要執行;
&&
短路與(&&)是,如果前面有條件已經回傳了false,不再往后判斷,最終結果是false;
對于或操作,有一個條件滿足結果就是true
|
普通或( | ),所有判斷條件都要執行;
||
短路或( || ),如果前面有條件回傳了true,不再向后判斷,最終結果是true;
6.一位陣列
可以把”int[ ]”在整體上看成一種資料型別,其他float[],double[]等也一樣
//錯誤的定義方式如下
int arr[4] = {34,23,12,54};
int[] arr = {43,23,12,11};
//正確的定義方式如下
資料型別[] 陣列名;//宣告一位陣列
陣列名 = new 資料型別[個數]; //分配記憶體給陣列
int[] arr;
arr = new int[4];
還可以這樣
資料型別[] 陣列名 = new 資料型別[個數];
int[] arr = new int[4];
也可以這樣
int[] arr = {2,3,2,1};//編譯器按照元素個數自動分配記憶體大小
7.亂數
import java.util.Random;
public class randomDemo
{
public static void main(String[] args)
{
Random rand = new Random();//創建一個random物件
int a = rand.nextInt(10);//回傳[0,10)的隨機整數
int b = rand.nextInt()*10 + 1;//回傳[0,10]的隨機整數
}
}
8.二維陣列
//三種宣告方式
//1
資料型別[][] 陣列名;
陣列名 = new 資料型別[行的個數][列的個數];
//2
資料型別[][] 陣列名 = new 資料型別[行的個數][列的個數];
//3
資料型別[][] 陣列名 = {
{第0行初值},
{第1行初值},
...
{第n行初值},
};
int[][] num = {
{2,3,32},
{4,3,2}
};
每行的元素個數可以不同
//1
int[][] num = {
{2,3,2,32},
{2,3},
{1,2,3,2,1,12,32}
};
//2
int[][] num = null;
num = new int[3][];
num[0] = new int[3];
num[1] = new int[10];
num[2] = new int[1];
9.null的使用
不可以將null賦給基本資料型別(如:int,float,double等)
比如,下面的形式是錯誤的:
int a = null;
下面的形式正確的:
Object a = null;//物件
10.物件的比較( "=="運算子 和 equals()方法的區別 )
1)"=="運算子用于比較兩個物件的記憶體地址是否相等,
2)equals()方法用于比較兩個物件的內容是否一樣
public class CompareObject1
{
public static void main(String[] args)
{
String str1 = new String("Java");
String str2 = new String("Java");
String str3 = str2;
if(str1 == str2)
{
System.out.println("str1 == str2");
}else{
System.out.println("str1 != str2");
}
if(str2 == str3)
{
System.out.println("str2 == str3");
}else{
System.out.println("str2 != str3");
}
}
}
輸出結果如下
str1 != str2
str2 == str3
public class CompareObject2
{
public static void main(String[] args)
{
String str1 = new String("Java");
String str2 = new String("Java");
String str3 = str2;
if(str1.equals(str2))
{
System.out.println("str1 equals str2");
}else{
System.out.println("str1 not equals str2");
}
if(str2.equals(str3))
{
System.out.println("str2 equals str3");
}else{
System.out.println("str2 not equals str3");
}
}
}
輸出結果如下
str1 equals str2
str2 equals str3
11.構造方法
1)構造方法不能被static和final修飾
2)構造方法不能被繼承,子類使用父類的構造方法需要使用super關鍵字
3)類名() -->這也是一個構造方法
class book
{
String bookName;//資料成員
String author;
int price;
public book()//建構式
{
bookName = "小王子";
author = "eksan";
price = 999;
}
public book(String bookName,String author,int price)//建構式
{
this.bookName = bookName;
this.author = author;
this.price = price;
}
public String show() {
return "書名:" + bookName +
"\n作者:" + author +
"\n價格:" + price;
}
}
public class bookDemo {
public static void main(String[] args){
// TODO Auto-generated method stub
book b1 = new book();//book()就是一個建構式,創建了一個book類成員變數b1
book b2 = new book("回到過去","Eksan",666);//book("回到過去","Eksan",666)就是一個建構式,創建了一個book類成員變數b2
System.out.println(b1.show());
System.out.println();
System.out.println(b2.show());
}
}
// 結果如下
書名:小王子
作者:eksan
價格:999
書名:回到過去
作者:Eksan
價格:666
12.代碼塊
可以有多個代碼塊
1)普通代碼塊
方法體內用一堆大括號括起來的代碼區間,
只能放在方法體內
public class Putong {
public static void main(String[] args){
// "{}" 括號括起來的就是普通的代碼塊
{
int x = 10;
System.out.println("普通的代碼塊內,x = " + x);
}
int x = 100;
System.out.println("x = " + x);
}
}
// 結果如下
普通的代碼塊內,x = 10
x = 100
2)構造代碼塊
在類中直接定義的,沒有任何修飾符的代碼塊
構造代碼塊和構造方法一樣,是在物件生成時被呼叫,但構造代碼塊呼叫時間比構造方法早
如果只有一個代碼塊,10個建構式,每一個建構式執行之前先執行構造代碼塊
class Person
{
private String name;
private int x;
//構造代碼塊
{
System.out.println("構造代碼塊執行.....");
x = 100;
}
//構造方法的代碼塊
Person()
{
System.out.println("構造方法執行.......");
name = "Eksan";
show();
}
//構造方法的代碼塊
Person(String name)
{
System.out.println("構造方法執行........");
this.name = name;
show();
}
void show() {
System.out.println("Welcome! " + name);
System.out.println("x = " + x);
}
}
public class Gouzao {
public static void main(String[] args){
Person p1 = new Person();
System.out.println("..........................");
Person p2 = new Person("Eksan");
}
}
//執行結果如下
構造代碼塊執行.....
構造方法執行.......
Welcome! Eksan
x = 100
..........................
構造代碼塊執行.....
構造方法執行........
Welcome! Eksan
x = 100
3)靜態代碼塊
使用static關鍵字加以修飾并用大括號括起來的代碼塊
最早執行的代碼塊之一
public class HelloWorld {
//靜態代碼塊
static
{
System.out.println("靜態代碼塊執行...........");
}
//構造方法
public HelloWorld() {
System.out.println("構造方法執行............");
}
//構造代碼塊
{
System.out.println("構造代碼塊執行...........");
}
public static void main(String[] args){
System.out.println("main方法開始執行.........");
System.out.println("創建第1個物件............");
new HelloWorld();
System.out.println("創建第2個物件............");
new HelloWorld();
System.out.println("創建第3個物件............");
new HelloWorld();
}
}
// 執行結果如下
靜態代碼塊執行...........
main方法開始執行.........
創建第1個物件............
構造代碼塊執行...........
構造方法執行............
創建第2個物件............
構造代碼塊執行...........
構造方法執行............
創建第3個物件............
構造代碼塊執行...........
構造方法執行............
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/256361.html
標籤:java
上一篇:JVM學習-位元組碼指令
