基于理解的java學習筆記之最最基礎部分
最最基礎部分
保留關鍵字:goto、const
增加關鍵字:assert、enum
public class SimpleDemo02{// 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
double x = 30.3 ; // 聲明小數
float y = 30.3f ; // 聲明小數
}
};
最大加1成最小
最小減1成最大
public class SimpleDemo{ // 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
int max = Integer.MAX_VALUE ; // 求出整型的最大值
int min = Integer.MIN_VALUE ; // 求出整型的最小值
System.out.println("int的最大值:" + max) ;
System.out.println("int的最大值 + 1:" + (max+1)) ;
System.out.println("int的最小值:" + min) ;
System.out.println("int的最小值 - 1:" + (min-1)) ;
}
}
數據類型的轉換:
public class SimpleDemo03{// 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
char c1 = 'a' ;// 使用”'“括起來表示字符
int x1 = c1 ; // 將char變為int型
x1++ ; // 自增
char c2 = (char)x1 ; // 將int --> char,進行強制轉換
System.out.println(c2) ;
}
}
char→int
byte→short→int→long
int→float→double
反過來,需進行強制轉換
轉換時如碰到的是字串符,則所有類型都向字串符轉換
public class SimpleDemo04{// 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
String str = "hello " ; // 表示字符串
int i = 10 ;
System.out.println(str + (i + 20)) ;
System.out.println(1l + 11) ;
}
}
位操作:指使用二進制代碼完成的數據操作
&按位與
|按位或
^異或(相同為0,不同為1)
~取反
<<左位移(有符號)
>>右位移(有符號)
>>>無符號右位移
public class SimpleDemo05{// 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
int x = 3 ;
int y = 6 ;
System.out.println(x & y) ;
System.out.println(x | y) ;
System.out.println(x ^ y) ;
}
}
3的二進制代碼:00000000 00000000 00000000 00000011
6的二進制代碼:00000000 00000000 00000000 00000110
反碼:反碼所有的位按位取反,整數的反碼就是其本身,負數的反碼是用補碼的形式表示出來,再進行取反操作,補碼=反碼+1
public class SimpleDemo06{// 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
int x = -3 ;
System.out.println(~x) ;
}
}
-3的反碼是2
3的反碼是-4
public class SimpleDemo07{// 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
int x = 3 ;
System.out.println(x<<2) ; // 左移兩位
}
}
public class SimpleDemo08{// 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
int x = 3 ;
int y = -3 ;
System.out.println(x>>2) ; // 右移兩位
System.out.println(y>>2) ; // 右移兩位
}
}
public class SimpleDemo09{// 聲明一個類,類名稱的命名規范:所有單詞的首字母大寫
public static void main(String args[]){ // 主方法
int x = 3 ;
int y = -3 ;
System.out.println(x>>>2) ; // 右移兩位
System.out.println(y>>>2) ; // 右移兩位
}
}
循環控制
分支結構:if、if…else、if…else if…else
循環結構:while、do…whlie、for
循環控制:break、continue
數組
1. 聲明時直接開辟內存空間
數據類型數組名稱[]=new 數據類型[長度];
或 數據類型 [] 數組名稱=new 數據類型[長度];
2.分步聲明
聲明數組:數據類型數組名稱[]=null;
實例化數組:數組名稱=new 數據類型[長度];
public class ArrayDemo01{
public static void main(String args[]){
int i[] = null ;
// i = new int[10] ; // 開辟了10個空間大小的數組
System.out.print("數組開辟之后的內容:") ;
for(int x=0;x<i.length;x++){
System.out.print(i[x] + "、") ;
}
i[0] = 30 ; // 為第一個元素賦值
i[9] = 60 ; // 為最后一個元素賦值
System.out.print(""n數組賦值之后的內容:") ;
for(int x=0;x<i.length;x++){
System.out.print(i[x] + "、") ;
}
}
}
為數組的元素賦值,需通過下標的形式進行訪問
靜態初始化:
public class ArrayDemo02{
public static void main(String args[]){
int i[] = {1,2,3,4,6,7} ; // 此時屬于靜態初始化
System.out.print("數組開辟之后的內容:") ;
for(int x=0;x<i.length;x++){
System.out.print(i[x] + "、") ;
}
}
}
二維數組
public class ArrayDemo03{
public static void main(String args[]){
int i[][] = {{1,2},{2,3,4},{3,4,5,6,7}} ; // 此時屬于靜態初始化
System.out.print("數組開辟之后的內容:") ;
for(int x=0;x<i.length;x++){
for(int y=0;y<i[x].length;y++){
System.out.print(i[x][y] + "、") ;
}
System.out.println("") ;
}
}
}
主方法之外的方法:可重復調用的代碼段
public class MethodDemo01{
public static void main(String args[]){
print() ; // 調用方法
print() ; // 調用方法
print() ; // 調用方法
}
public static void print(){
System.out.println("Hello World!!!") ;
}
}
public class MethodDemo02{
public static void main(String args[]){
System.out.println(add(10,20)) ;
System.out.println(add(30,30)) ;
}
public static int add(int x ,int y){
int temp = x + y ;
return temp ; // 將計算結果返回
}
}
此內容包含方法的重載
指過程中調用的方法名稱相同,但是參數的類型或個數不同(同樣方法也被聲明多個,但方法都相同)
public class MethodDemo03{
public static void main(String args[]){
System.out.println(add(10,20)) ;
System.out.println(add(30,30)) ;
System.out.println(add(30,30,30)) ;
System.out.println(add(30.03f,30.01f)) ;
}
public static int add(int x ,int y){
int temp = x + y ;
return temp ; // 將計算結果返回
}
public static int add(int x ,int y,int z){
int temp = x + y + z ;
return temp ; // 將計算結果返回
}
public static float add(float x ,float y){
float temp = x + y ;
return temp ; // 將計算結果返回
}
}
public class MethodDemo04{
public static void main(String args[]){
System.out.println(1) ;
System.out.println("hello") ;
System.out.println(1.1) ;
System.out.println('c') ;
}
}
System.out.println()可接受任意形式的參數,屬于方法的重載
但要注意
public class MethodDemo05{
public static void main(String args[]){
}
public static int add(int x ,int y){
int temp = x + y ;
return temp ; // 將計算結果返回
}
public static float add(int x ,int y){
float temp = x + y ;
return temp ; // 將計算結果返回
}
}
不是方法的重載,因為此情況下雖然返回值不同,但參數的類型和個數相同
return的使用:使用return結束一個方法的操作,當執行到return時直接返回到方法調用處繼續執行
public class MethodDemo06{
public static void main(String args[]){
fun(10) ;
fun(3) ;
}
public static void fun(int x){
System.out.println("進入方法。") ;
if(x==3){
return ;// 返回方法的被調用處
}
System.out.println("結束方法。") ;
}
}
遞歸調用:自己調用自己
public class MethodDemo07{
public static void main(String args[]){
int sum = 0 ;
for(int i=0;i<=100;i++){
sum += i ; // 累加操作
}
System.out.println("計算結果:" + sum) ;
}
}
但遞歸操作通常要有明確的截止條件,否則會出現異常
public class MethodDemo08{
public static void main(String args[]){
int sum = 0 ;
sum = fun(100) ;
System.out.println("計算結果:" + sum) ;
}
public static int fun(int temp){
if(temp==1){
return 1 ;
}else{
return temp + fun(temp-1) ;
}
}
}
方法與數組
方法中對數組所作的一切修改,最終都會被保留下來
public class MethodArrayDemo01{
public static void main(String args[]){
int temp[] = {1,3,5,7,9} ; // 聲明數組
fun(temp) ;
print(temp) ;
}
public static void fun(int []x){
x[0] = 6 ; // 修改第一個元素
}
public static void print(int[] x){
for(int i=0;i<x.length;i++){
System.out.print(x[i] + "、") ;
}
}
}
使用方法可以返回一個數組,只要在返回值類型上加入數組類型即可
public class MethodArrayDemo02{
public static void main(String args[]){
int temp[] = fun() ;// 聲明數組
print(temp) ;
}
public static int[] fun(){
int x[] = {1,3,5,7,9} ;
return x ;// 返回數組
}
public static void print(int[] x){
for(int i=0;i<x.length;i++){
System.out.print(x[i] + "、") ;
}
}
}
排序操作
public class MethodArrayDemo03{
public static void main(String args[]){
int temp[] = fun() ;// 聲明數組
java.util.Arrays.sort(temp) ; // 進行排序操作
print(temp) ;
}
public static int[] fun(){
int x[] = {23,1,5,3,24,3,56,4,3,1} ;
return x ;// 返回數組
}
public static void print(int[] x){
for(int i=0;i<x.length;i++){
System.out.print(x[i] + "、") ;
}
}
}
拷貝操作
public class MethodArrayDemo04{
public static void main(String args[]){
int t1[] = {1,2,3,4,5,6,7,8,9} ;
int t2[] = {11,22,33,44,55,66,77,88,99} ;
// 源數組名稱下標目標數組下標拷貝長度
System.arraycopy(t2,0,t1,3,3) ; // 數組拷貝
print(t1) ;
}
public static void print(int[] x){
for(int i=0;i<x.length;i++){
System.out.print(x[i] + "、") ;
}
}
}
新特性對數組的支持
foreach
for(數據類型變量:數組){
//操作
}
public class MethodArrayDemo05{
public static void main(String args[]){
int t1[] = {1,2,3,4,5,6,7,8,9} ;
for(int x:t1){
System.out.print(x + "、") ;
}
}
}
可變參數
聲明方法參數時,參數個數不固定,則使用也不固定
public static 返回值類型數組名稱(數據類型…參數名稱)
public class MethodArrayDemo06{
public static void main(String args[]){
int temp[] = {2,4,6,8} ;
fun() ; // 沒有參數
fun(1) ; // 一個參數
fun(1,3,5,7,9) ; // 一個參數
fun(temp) ;
}
public static void fun(int ... arg){
for(int x:arg){
System.out.print(x + "、") ;
}
System.out.println() ;
}
}
對象的創建及使用
類通過屬性建立模型,而對象將模型實例化,方法則是完成任務使用的便利工具
類名對象名稱=null; //聲明對象
對象名稱=new 類名(); //實例化對象
或直接實例化 類名對象名稱=new 類名();
class Person{
String name ; // 表示人的姓名
int age ; // 表示人的年齡
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + name + ",年齡:" + age) ;
}
};
public class OODemo02{
public static void main(String args[]){
Person per = new Person() ; // 產生實例化對象
}
}
訪問類中的屬性和方法
訪問屬性:對象名稱.屬性名=值
訪問方法:對象名稱.方法()
class Person{
String name ; // 表示人的姓名
int age ; // 表示人的年齡
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + name + ",年齡:" + age) ;
}
};
public class OODemo03{
public static void main(String args[]){
Person per = new Person() ; // 產生實例化對象
per.name = "張三" ; // 為名字賦值
per.age = 30 ; // 為年齡賦值
per.tell() ; // 調用方法
}
}
引用傳遞
將一個堆內存空間的使用權交給其他對象,相當于為一個堆內存空間起了一個別名
class Person{
String name ; // 表示人的姓名
int age ; // 表示人的年齡
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + name + ",年齡:" + age) ;
}
};
public class OODemo06{
public static void main(String args[]){
Person per1 = null ; // 聲明對象
Person per2 = null ; // 聲明對象
per1 = new Person() ; // 實例化對象
per2 = per1 ; // 引用傳遞
per1.name = "張三" ; // 為名字賦值
per1.age = 30 ; // 為年齡賦值
per2.age = 33 ;
per1.tell() ; // 調用方法
per2.tell() ;
}
}
class Person{
String name ; // 表示人的姓名
int age ; // 表示人的年齡
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + name + ",年齡:" + age) ;
}
};
public class OODemo07{
public static void main(String args[]){
Person per1 = null ; // 聲明對象
Person per2 = null ; // 聲明對象
per1 = new Person() ; // 實例化對象
per2 = new Person() ; // 實例化對象
per2.name = "李四" ;
per2 = per1 ; // 引用傳遞
per1.name = "張三" ; // 為名字賦值
per1.age = 30 ; // 為年齡賦值
per2.age = 33 ;
per1.tell() ; // 調用方法
per2.tell() ;
}
}
一個棧內存只能引用一個堆內存,但一個堆內存中可以被多個棧內存所指向
封裝屬性:private 數據類型屬性名稱=默認值;
封裝方法:private 返回值類型|void 方法名稱(參數列表)
class Person{
private String name; // 表示人的姓名
private int age ; // 表示人的年齡
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + name + ",年齡:" + age) ;
}
};
public class OODemo09{
public static void main(String args[]){
Person per = new Person() ; // 實例化對象
per.name = "張三" ; // 為name屬性賦值
per.age = -30;
per.tell() ;
}
}
無法執行
固有:只要屬性就必須封裝,被封裝的屬性通過setter及getter方法設置和取得
class Person{
private String name; // 表示人的姓名
private int age ; // 表示人的年齡
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + this.getName() + ",年齡:" + this.getAge()) ;
}
public void setName(String n){
name = n ;
}
public void setAge(int a){
if(a>=0&&a<=200){
age = a ;
}
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
};
public class OODemo10{
public static void main(String args[]){
Person per = new Person() ; // 實例化對象
per.setName("張三") ; // 為name屬性賦值
per.setAge(-30);
per.tell() ;
}
}
this.方法()→調用本類方法
類的圖形,power designer工具
構造方法,為類中屬性進行初始化,每一個類中都存在一個構造方法
構造方法必與類名一致,定義時不能有返回值聲明,也不能在中用return返回一個內容
class Person{
private String name; // 表示人的姓名
private int age ; // 表示人的年齡
public Person(){ // 定義了一個構造方法
System.out.println("******************") ;
}
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + this.getName() + ",年齡:" + this.getAge()) ;
}
public void setName(String n){
name = n ;
}
public void setAge(int a){
if(a>=0&&a<=200){
age = a ;
}
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
};
public class OODemo11{
public static void main(String args[]){
Person per = null ;
per = new Person() ; // 實例化對象
/*
per.setName("張三") ; // 為name屬性賦值
per.setAge(30);
per.tell() ;
*/
}
}
class Person{
private String name; // 表示人的姓名
private int age ; // 表示人的年齡
public Person(){}
public Person(String n){
this.setName(n) ;
}
public Person(String n,int a){ // 定義了一個構造方法
this.setName(n) ; // 調用setName()方法
this.setAge(a) ; // 調用setAge()方法
System.out.println("******************") ;
}
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + this.getName() + ",年齡:" + this.getAge()) ;
}
public void setName(String n){
name = n ;
}
public void setAge(int a){
if(a>=0&&a<=200){
age = a ;
}
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
};
public class OODemo12{
public static void main(String args[]){
Person per = null ;
per = new Person("張三",-30) ; // 實例化對象
per.tell() ;
}
}
匿名對象,只使用一次的對象
class Person{
private String name; // 表示人的姓名
private int age ; // 表示人的年齡
public Person(){}
public Person(String n){
this.setName(n) ;
}
public Person(String n,int a){ // 定義了一個構造方法
this.setName(n) ; // 調用setName()方法
this.setAge(a) ; // 調用setAge()方法
System.out.println("******************") ;
}
public void tell(){ // 定義說話的方法
System.out.println("姓名:" + this.getName() + ",年齡:" + this.getAge()) ;
}
public void setName(String n){
name = n ;
}
public void setAge(int a){
if(a>=0&&a<=200){
age = a ;
}
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
};
public class OODemo13{
public static void main(String args[]){
new Person("張三",-30).tell() ;
}
}
使用
class Student{
private String name ;
private int age ;
private float english ;
private float computer ;
private float math ;
public Student(){}
public Student(String n,int a,float e,float c,float m){
this.setName(n) ;
this.setAge(a) ;
this.setEnglish(e) ;
this.setComputer(c) ;
this.setMath(m) ;
}
public float sum(){
return english + computer + math ;
}
public float avg(){
return this.sum() / 3 ;
}
public float max(){
float max = computer>math?computer:math ;
max = max>english?max:english ;
return max ;
}
public float min(){
float min = computer<math?computer:math ;
min = min<english?min:english ;
return min ;
}
public String getInfo(){
return "學生信息: "n" +
""t|- 姓名:" + this.getName() + ""n" +
""t|- 年齡:" + this.getAge() + ""n" +
""t|- 數學成績:" + this.getMath() + ""n" +
""t|- 英語成績:" + this.getEnglish() + ""n" +
""t|- 計算機成績:" + this.getComputer() ;
}
public void setName(String n){
name = n ;
}
public void setAge(int a){
age = a ;
}
public void setEnglish(float e){
english = e ;
}
public void setComputer(float c){
computer = c ;
}
public void setMath(float m){
math = m ;
}
public String getName(){
return name ;
}
public int getAge(){
return age ;
}
public float getEnglish(){
return english ;
}
public float getComputer(){
return computer ;
}
public float getMath(){
return math ;
}
}
public class ExecDemo{
public static void main(String args[]){
Student stu = new Student("張三",30,89.0f,91.0f,87.0f) ;
System.out.println("總分:" + stu.sum()) ;
System.out.println("平均分:" + stu.avg()) ;
System.out.println("最高分:" + stu.max()) ;
System.out.println("最低分:" + stu.min()) ;
System.out.println(stu.getInfo()) ;
}
}
class Address {
private String national ;
private String province ;
private String city ;
private String street ;
private String zipcode ;
public Address(){}
public Address(String n,String p,String c,String s,String z){
this.setNational(n) ;
this.setProvince(p) ;
this.setCity(c) ;
this.setStreet(s) ;
this.setZipcode(z) ;
}
public String getInfo(){
return "地址信息:" + ""n" +
""t|- 國家:" + this.getNational() + ""n" +
""t|- 省份:" + this.getProvince() + ""n" +
""t|- 城市:" + this.getCity() + ""n" +
""t|- 街道:" + this.getStreet() + ""n" +
""t|- 郵編:" + this.getZipcode() ;
}
public void setNational(String n){
national = n ;
}
public void setProvince(String p){
province = p ;
}
public void setCity(String c){
city = c ;
}
public void setStreet(String s){
street = s ;
}
public void setZipcode(String z){
zipcode = z ;
}
public String getNational(){
return national ;
}
public String getProvince(){
return province ;
}
public String getCity(){
return city ;
}
public String getStreet(){
return street ;
}
public String getZipcode(){
return zipcode ;
}
};
public class AddressDemo{
public static void main(String args[]){
Address add = new Address("中國","北京","北京市","西城區","100088") ;
System.out.println(add.getInfo()) ;
}
}
posted on 2009-07-29 18:20 swedenborg 閱讀(312) 評論(1) 編輯 收藏