ORACLE官网JAVA学习文档
Trails Covering the Basics
Figure 1 an overview of the software development process
Figure 3 java平台的两个组件
java MyApp arg1 arg2
class Bicycle{
int cadence=0;
int speed=0;
int gear=1;
void changeCadence(int newValue)
{
cadence=newValue;
}
void changeGear(int newValue)
{
gear=newValue;
}
void speedUp(int increment)
{
speed=speed+increment;
}
void applyBrakes(int increment)
{
speed=speed-decrement;
}
void printStates()
{
System.out.println("cadence:" +
cadence + " speed:" +
speed + " gear:" + gear);
}
}
class BicycleDemo{
public static void main(Stirng[] args)
{
Bicycle bicycle1= new Bicycle();
Bicycle bicycle2= new Bicycle();
bicycle1.changeCadence(50);
bicycle1.speedUp(10);
bicycle1.printStates();
bicycle2.changeCadence(60);
bicycle2.changeGear(20);
bicycle2.speedUp(10);
bicycle2.printStates();
}
}
class MountainBike extends Bicycle
{
//new fieds and methods defining
//a mountaion bike would go there
}
interface Bicycle
{
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
class ACMBicycle implements Bicycle
{
int cadence=0;
int speed=0;
int gear=1;
void changeCadence(int newValue)
{
cadence=newValue;
}
void changeGear(int newValue)
{
gear=newValue;
}
void speedUp(int increment)
{
speed=speed+increment;
}
void applyBrakes(int decrement)
{
speed=speed-decrement;
}
void printSatas()
{
System.out.println("cadence"+cadence+"speed"+speed+"gear"+gear)
}
}
|
byte
|
8-bit
|
|
Data type
|
Default value(for fields)
|
|
byte
|
0
|
|
short
|
0
|
|
int
|
0
|
|
long
|
0L
|
|
float
|
0.0f
|
|
double
|
0.0d
|
|
char
|
'\u0000'
|
|
String(or any object)
|
null
|
|
boolean
|
false
|
|
float
|
以F或者f结尾
|
|
double
|
以D或者d结尾
|
|
char
|
single quote
|
|
String
|
double quote
|
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
int[] anArrayOfInts;
long[] anArratOfLongs;
float anArrayOfFloats[]
anArray=new int[10]
int[] anArray=
{
100,20,40,50,
500,12,12,14
}
public static void arrraycopy(Object src, int srcPro
Object dest, int destPos, int length
)
public class Array {
public static void main(String[] argv)
{
char[] copyFrom={'d','e','f','a','b','c','d'};
char[] copyTo=new char[4];
System.arraycopy(copyFrom,3,copyTo,0,4);
System.out.println(new String(copyTo));
}
}
class ArrayCopyDemo
{
public static void main(String[] args)
{
char[] copyForm={'a','b','c','d','e'}
char[] copyTo= java.util.Arrays.copyOfArrays(copyForm,1,4);
System.out.println(new String(copyTo));
}
}
- Searching an array for a specific value to get the index at which it is placed (the
binarySearch
- method).
- Comparing two arrays to determine if they are equal or not (the
equals
- method).
- Filling an array to place a specific value at each index (the
fill
- method).
- Sorting an array into ascending order. This can be done either sequentially, using the
sort
- method, or concurrently, using the
parallelSort
- method introduced in Java SE 8. Parallel sorting of large arrays on multiprocessor systems is faster than sequential array sorting.
|
Operator
|
Description
|
|
+
|
Additive
|
|
-
|
Subtraction operator
|
|
*
|
Multiplication operator
|
|
/
|
Division operator
|
|
%
|
Remainder operator
|
|
Operator
|
Description
|
|
+
|
Unary plus operator
|
|
-
|
Unary minus operator
|
|
++
|
Increment operator
|
|
--
|
Decrement operator
|
|
!
|
Logical complement operator
|
|
==
|
equal to
|
|
!=
|
not equal to
|
|
>
|
gearter than
|
|
>=
|
greater than or equal to
|
|
<
|
less than
|
|
<=
|
less than or equal to
|
public class Array {
public static void main(String[] argv)
{
int value1=1;
int value2=2;
if(value1==value2)
{
System.out.println("value1==value2");
}
if(value1!=value2)
{
System.out.println("value1!=value2")
}
if(value1>value2)
{
System.out.println("value1>vluae2");
}
if(value1<value2)
{
System.out.println("value1<value2");
}
}
}
public class ConditionalDemo1 {
public static void main(String[] argv)
{
int value1 = 1;
int value2 = 2;
if((value1==1)&&(value2==2))
{
System.out.println("value1 is 1 AND value2 is 2");
}
if((value1==1)||(value2==2))
{
System.out.println("value1 is 1 OR value2 is 1");
}
}
}
class ConditionalDemo2 {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
int result;
boolean someCondition = true;
result = someCondition ? value1 : value2;
System.out.println(result);
}
}
public class InstanceofDemo {
public static void main(String[] args)
{
Parent parent1 = new Parent();
Child child = new Child();
System.out.println("parent1 instanceof Parent "+(parent1 instanceof Parent));
System.out.println("parent1 instanceof Child "+(child instanceof Child));
System.out.println("parent1 instanceof MyInterface "+(parent1 instanceof MyInterface));
System.out.println("child instanceof Parent "+(child instanceof Parent));
System.out.println("child instanceof Child "+(child instanceof Child));
System.out.println("child instanceof MyInterface "+(child instanceof MyInterface));
}
}
class Parent{}
class Child extends Parent implements MyInterface{}
interface MyInterface{}
|
bitwise &
|
bitwise AND operation
|
|
bitwise ^
|
exclusive OR operation 位互斥或操作符
|
|
bitwise |
|
inclusive OR operation 位包换或操作符
|
class BitDemo {
public static void main(String[] args) {
int bitmask = 0x000F;
int val = 0x2222;
// prints "2"
System.out.println(val & bitmask);
}
}
|
=
|
Simple assignment operator
|
|
+
|
Additive operator(also used for String concatenation)
|
|
-
|
Subtraction operator
|
|
*
|
Multiplication operator
|
|
/
|
Division operator
|
|
%
|
Remainder operator
|
|
+
|
Unary plus operator;indicates positive value
|
|
-
|
Unary minus operator;negates an exprssion
|
|
++
|
Increment operator
|
|
==
|
Equal to
|
|
!=
|
Not equal to
|
|
>
|
Greater than
|
|
>=
|
Greater than or equal to
|
|
<
|
Less than
|
|
<=
|
Less than or equal to
|
|
&&
|
Conditional-And
|
|
||
|
Conditional-OR
|
|
?:
|
Ternary
|
|
instanceof
|
Compares an object to a specified type
|
|
~
|
Unary bitwise complement
|
|
<<
|
Signed left shift
|
|
>>
|
Signed right shift
|
|
>>>
|
Unsigned right shift
|
|
&
|
Bitwise AND
|
|
^
|
Bitwise exclusive OR
|
|
|
|
Bitwise incluseive OR
|
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
public class SwithDemoFallThrough
{
public static void main(String[] args)
{
java.util.ArrayList<String> futureMonths = new java.util.ArrayList<String>();
int month = 8;
switch(month)
{
case 1: futureMonths.add("January");
case 2: futureMonths.add("February");
case 3: futureMonths.add("March");
case 4: futureMonths.add("April");
case 5: futureMonths.add("May");
case 6: futureMonths.add("June");
case 7: futureMonths.add("July");
case 8: futureMonths.add("August");
case 9: futureMonths.add("September");
case 10: futureMonths.add("October");
case 11: futureMonths.add("November");
case 12: futureMonths.add("December");
break;
default: break;
}
if(futureMonths.isEmpty())
{
System.out.println("Invalid month number");
}
else
{
for(String monthName:futureMonths)
{
System.out.println(monthName);
}
}
}
}
while(expression)
{
statements
}
do
{
statement(s)
}while(expression)
for(initilization;termination;increment)
{
statement(s)
}
public class EnhancedForDemo {
public static void main(String[] args) {
int[] numbers={1,2,3,4,5,6,7,8};
for(int item: numbers)
{
System.out.println(item);
}
}
}
public class BreakWithLabelDemo {
public static void main(String[] args) {
int[][] arrayOfInts=
{
{32, 87, 3, 589 },
{1, 1076, 2000, 8},
{622, 127, 77, 955}
};
int searchfor =12;
int i;
int j=0;
boolean foundIt= false;
search:for(i=0;i<arrayOfInts.length;i++)
{
for(j=0;j<arrayOfInts[i].length;j++)
{
if(arrayOfInts[i][j]==searchfor)
{
foundIt=true;
break search;
}
}
}
if(foundIt==true)
{
System.out.println("found it!");
}
else
{
System.out.println("not found it!");
}
}
}
public class ContinueWithLabelDemo
{
public static void main(String[] args)
{
String searchMe = "Look for a substring in me";
String substring = "sub";
boolean foundIt = false;
int max=searchMe.length()-substring.length();
test:for(int i=0;i<=max;i++)
{
int n=substring.length();
int j=i;
int k=0;
while(n-- !=0)
{
if(searchMe.charAt(j++)!=substring.charAt(k++))
{
continue test;
}
}
if(foundIt=true)
{
break test;
}
}
if(foundIt)
{
System.out.println("found it");
}
else
{
System.out.println("Not found it");
}
}
}
class MyClass
{
//fields,constructor, and
//method declarations
}
class MyClass extends MySuperClass implements YourInterface
{
//field, constructor, and
//method declarations
}
- 类中的成员变量-叫做字段
- 方法或者代码中的变量叫本地变量
- 字段声明叫做参数
public int cadence
public int gear
public int speed
public class PrivateBicycle
{
private int cadence;
private int gear;
private int speed;
public PrivateBicycle(int startCadence, int startSpeed, int startGear)
{
gear = startGear;
speed = startSpeed;
cadence = startCadence;
}
public int getCadence()
{
return cadence;
}
public int getGear()
{
return gear;
}
public int getSpeed()
{
return speed;
}
public void setCadence(int newValue)
{
cadence = newValue;
}
public void setGear(int newValue)
{
gear = newValue;
}
public void applyBrake(int decrement)
{
speed-=decrement;
}
public void speedUp(int increment)
{
speed+=increment;
}
}
- 修饰符 public , private
- 返回type
- 方法名
- 参数
- an exception list
- 方法体
public class DataArtist
{
public void draw(String s){}
public void draw(int i){}
public void draw(double f){}
public void draw(float f){}
}
Bicycle yourBike = new Bicycle()
public double computePayment(double loanAmt, double rate, double futureValue, int numPeriods)
{
double interest= rate/100.0;
double partial1= Math.pow((1+interest),-numPeriods);
double denominator = (1-partial1)/interest;
double answer = (-loanAmt/denominator)-((futureValue*partial1)/denominator);
return answer;
}
public Polygon polygonFrom(Point[] corners)
{
//method body goes there
}
public Ploygon ploygonFrom(Point... corners)
{
int numberOfSides = corners.length;
double squareOfSide1,lengthOfSides
squareOfSide1 = (corners[1].x-corners[0].x)*(corners[1].x-corners[0])+(corners[1].y-corners[0].x)*(corners[1].y-corners[0].y)
lengthOfSides=Math.sqrt(squareOfSide1);
}
public PrintStream printf(Stirng format,Object... args)
Point originOne = new Point(20,30);
int height = new Rectangle().height;
Rectangle rectOne = new Rectangle(originOne,100,200)
new Rectangle(100,50).getArea()
- Returning values from methods
- The this key word
- Class vs. instance members
- Access control
- 运行完所有方法
- 抵达return 语句
- 抛出异常
public Number returnANumber{
...
}
public ImaginaryNumber returnANumber() {
...
}
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
...
}
public static int getNumberOfBicycles
{
return numberOfBicycles;
}
- 实例方法可以直接访问实例方法和实例变量
- 实例方法可以直接访问类变量和类方法;
- 类方法可以直接访问类方法和类变量;
- 类方法不能直接访问实例变量和实例方法,他们必须使用一个对象引用。也不能使用this关键字。因为没有this所指引的实例。
static final double PI = 3.141592653589793;
public class Bicycle
{
public static void main(String[] args) {
System.out.println(Bicycle.getNumberOfBicycle());
}
private int cadence;
private int gear;
private static int numberOfBicycle=2;
public int getCadence() {
return cadence;
}
public void setCadence(int cadence) {
this.cadence = cadence;
}
public int getGear() {
return gear;
}
public void setGear(int gear) {
this.gear = gear;
}
public static int getNumberOfBicycle() {
return numberOfBicycle;
}
public static void setNumberOfBicycle(int numberOfBicycle) {
Bicycle.numberOfBicycle = numberOfBicycle;
}
}
public class BedAndBreakfast {
public static int capacity=10;
private boolean full=false;
}
static
{
// whatever code is needed for initialization goes here
}
class Whatever
{
public static varType myVar=initializeClassVariable();
private static varType initializeClassVariable()
{
//initialization code goes here
}
}
{
// whatever code is needed for initialization goes here
}
class Whatever
{
private varType myVar=initializeInstanceVariable();
protected final varType initializeInstanceVariable()
{
// initialization code goes here
}
}
public class OuterClass {
...
class NestedClass
{
...
}
}
class OuterClass
{
...
static class StaticNestedClass
{
...
}
class innerClass
{
...
}
}
OuterClass.StaticNestedClass
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass
class OuterClass
{
...
class InnerClass
{
...
}
}
OuterClass.InnerClass innerObject= outerObject.new InnerClass();
public class ShowTest
{
public int x=0;
class FirstLevel
{
public int x=1;
void methodInFirstLevel
{
System.out.println(x);
System.out.println(this.x);
System.out.println(ShowTest.this.x);
}
}
public static void main(String args)
{
ShadowTest st = new ShadowTest();
ShadowTest.FirstLevel fl = st.new FirstLevel();
fl.methodInFirstLevel(23);
}
}
x = 23
this.x = 1
ShadowTest.this.x = 0
public class DataStructure {
//create an array
private final static int SIZE =15;
private int[] arrayOFInts = new int[SIZE];
public void printEven()
{
DataStructureIterator iterator = this.new EvenIterator();
while(iterator.hasNext())
{
System.out.println(iterator.next()+" ");
}
System.out.println();
}
interface DataStructureIterator extends java.util.Iterator<Integer>{}
private class EvenIterator implements DataStructureIterator
{
private int nextIndex =0;
public boolean hasNext()
{
return (nextIndex<=SIZE-1);
}
public Integer next()
{
Integer retValue=Integer.valueOf(arrayOFInts[nextIndex]);
nextIndex+=2;
return retValue;
}
}
public static void main(String[] args) {
DataStructure ds = new DataStructure();
ds.printEven();
}
}
ORACLE官网JAVA学习文档的更多相关文章
- dubbo官网和帮助文档
dubbo官网和帮助文档 https://github.com/apache/incubator-dubbo 内含帮助文档: http://dubbo.apache.org/books/dubbo-d ...
- Nmap官网中众多文档如何查看
打开Nmap(nmap.org)官网后,会看多个关于文档的链接,熟悉之后会发现有三类,Reference Guide,Books,Docs.通过熟悉知道Doc是文档的入口,且下面是对Doc页面的翻译, ...
- Oracle官网下载参考文档
最近有人问我有没有Oracle11g数据库官方参考文档,我就想,这不是在官网可以下载到的吗,疑惑,问了之后才知道,他官网找过,但时没有找到.不要笑,其实有很多人一样是找不到的,下面就一步一步操作下: ...
- 解决linux 无法下载 oracle 官网 java的 安装包
wget --no-cookies --no-check-certificate --header "Cookie: oraclelicense=accept-securebackup-co ...
- 官网Android离线文档下载
这是Android的离线API及一些Guide——俗称的/docs文件夹下的内容——英文版的...——http://pan.baidu.com/s/1qXmLlQc
- 如何从sun公司官网下载java API文档(转载)
相信很多同人和我一样,想去官网下载一份纯英文的java API文档,可使sun公司的网站让我实在很头疼,很乱,全是英文!所以就在网上下载了别人提供的下载!可是还是不甘心!其实多去看看这些英文的技术网站 ...
- 从Oracle官网学习oracle数据库和java
网上搜索Oracle官网:oracle官网 进入Oracle官网 点击menu-Documentation-Java/Database,进入Oracle官网的文档网站 首先是Java,可以看到Java ...
- 如何在Oracle官网下载java的JDK最新版本和历史版本
官网上最显眼位置只显示了Java SE的JDK的最新版本下载链接,因为都是英文,如果英文不是很好,寻找之前的JDK版本需要很长时间,而且未必能在那个隐蔽的位置找到之前版本列表. 今天小编来给你详细讲解 ...
- soapUI学习文档(转载)
soapUI 学习文档不是前言的前言记得一个搞开发的同事突然跑来叫能不能做个WebService 性能测试,当时我就凌乱了,不淡定啊,因为我是做测试的,以前连WebService 是什么不知道,毕竟咱 ...
随机推荐
- 【错误】【vscode】"'#' not expected here"
今天使用vscode发现完整的代码报错了,但依然可以运行
- collection介绍
1.collection介绍 在mongodb中,collection相当于关系型数据库的表,但并不需提前创建,更不需要预先定义字段 db.collect1.save({username:'mayj' ...
- Spring1
一.Spring是什么?有什么用? Spring的适用环境是这样的,假设现在有一个类port,它将提供一个返回消息的功能,代码如下: public class port { private weibo ...
- 201412-2 Z字形扫描(c语言)
问题描述 在图像编码的算法中,需要将一个给定的方形矩阵进行Z字形扫描(Zigzag Scan).给定一个n×n的矩阵,Z字形扫描的过程如下图所示: 对于下面的4×4的矩阵, 1 5 3 9 3 7 5 ...
- MBR和EFI启动过程
MBR启动过程 BIOS-->MBR(主引导记录)-->DPT(硬盘分区表)-->DBR(分区引导扇区)-->BootMgr-->BCD-->Winload.exe ...
- 苹果电脑基本设置+Linux 命令+Android 实战集锦
本文微信公众号「AndroidTraveler」首发. 背景 大多数应届毕业生在大学期间使用的比较多的是 windows 电脑,因此初入职场如果拿到一台苹果电脑,可能一时间不能够很快的上手.基于此,这 ...
- Go-cron定时任务
1.cron(计划任务) 按照约定的时间,定时的执行特定的任务(job). cron 表达式 表达了这种约定. cron 表达式代表了一个时间集合,使用 6 个空格分隔的字段表示. 秒 分 时 日 月 ...
- 二、Ansible的Ad-hoc介绍篇
一.什么是Ad-hoc 称为临时命令,简单说,就是在命令行界面,直接通过一条ansible命令,去指定主机执行指定指令,功能有限 例如:ansible localhost -m command -a ...
- 搭建本地的idea激活服务器
前言 博主用的是idea这个IDE,因为最近idea官方打击第三方激活服务有些严重,所以我的idea经常处于今天可以用,到了明天就不能用的状态,所以,从idea激活的网站找到了本地的id ...
- Shrio使用Jwt达到前后端分离
概述 前后端分离之后,因为HTTP本身是无状态的,Session就没法用了.项目采用jwt的方案后,请求的主要流程如下:用户登录成功之后,服务端会创建一个jwt的token(jwt的这个token中记 ...