Java入门编程实例

      php的学习先告一段落了,现在拿出近几天看的视频教程中一些Java实例和大家分享。
         First Java Program
    class HelloWorld
  {
    public static void main(String[] args)
  {
     System.out.println("Hello World!");

   }     
       
  }

数值赋值:
class Number/*创建数字类*/{
int i;
}
public class Assignment{
 public static void main(String[] args){
Number n1=new Number();
Number n2=new Number();
n1.i=9;
n2.i=47;
System.out.println("1:n1.i:"+n1.i+",n2.i:"+n2.i);
n1=n2;/*两个指针指向同一个对象

*/
System.out.println("2:n1.i:"+n1.i+",n2.i:"+n2.i);
n1.i=27;/*对象如果改变都改变*/
System.out.println("3:n1.i:"+n1.i+",n2.i:"+n2.i);
}
}

关系运算符的使用:
public class Equivalence{
public static void main (String[] args){
Integer n1=new Integer(47);
Integer n2=new Integer(47);
System.out.println(n1==n2);
System.out.println(n1!=n2);
}
}

public class Add{
public static void main(String[] args){
int x=0,y=1,z=2;
String sString="x,y,z";
System.out.println(sString+x+y+z);
System.out.println(y+z+sString);
}
}

public class LabelFor{
public static void main(String[] args){
int i=0;
outer:/*给循环命名*/
for(;ture;){//infinite loop
inner:/*给循环命名*/
for(;i<4;i++){
prt("i="+i);
if(i==1){
prt("continue");
continue;
}
if(i==2){
prt("continue outer");
i++;
continue outer;
}
if(i==3){
prt("break outer");
break outer;
}
}
}
}

用this关键字。
this
eg:
public class Leaf{
private int i=0;
Leaf increment(){
i++;
return this;
}
void print(){
System.out.println("i="+i);
}
public static void main (String[] args){
Leaf x=new Leaf();
x.increment().increment().increment().print();
}
}
 
public class Arrays{
public static void main (String[] args){
int[] a1={1,2,3,4,5};
int[] a2;
a2=a1;
for(int i=0;i<a2.length;i++)
a2[i]++;
for(int i=0;i<a1.length;i++)
prt("a1["+i+"]="+a1[i]);
}
static void prt(String s){
System.out.println(s);
}
}

class Base{
    int i=47;
    int f(){ return i;}
}
class Derived extends Base {
    int i=27;
    int g(){ return i;}
}
public class Text{
public static void main(String[] args){
Derived d=new Derived();
System.out.println(d.f());
}
}

public class ExceptionMethods{
public static void main(String[] args){
try{
throw new Exception("Here's my Exception!");
}catch(Exception e){
System.out.println("Caught Exception");
System.out.println("e.getMessage():"+e.getMessage());
System.out.println("e.toString():"+e.toString());
System.out.println("e.printStackTrace():");
e.printStackTrace();
}
}

class A extends Exception{}
class B extends A{}
abstract class I{
I() throws A;
void fevent() throws A;
void gent() throws B;
void hent();
}
class C extends Exception{}
class D extends B{}
interface II{
void fevent() throws C;
}
class CC extends I implements II{
CC() throws A,C{};
gent() throws D;
fevent();
}

class IO{
public static void main(String[] args){
byte buffer[]=new byte[2056];
try{
while(true){
int bytes=System.in.read(buffer,0,2056);
String str=new String(buffer,0,bytes);
System.out.println("-->"+str+":"+bytes+":"+str.length());
}
}
catch(Exception e){
String err=e.toString();
System.out.println(err);
}
}
}


施杨出品 !

你可能感兴趣的:(java,编程,C++,c,PHP)