欢迎来到冰点文库! | 帮助中心 分享价值,成长自我!
冰点文库
全部分类
  • 临时分类>
  • IT计算机>
  • 经管营销>
  • 医药卫生>
  • 自然科学>
  • 农林牧渔>
  • 人文社科>
  • 工程科技>
  • PPT模板>
  • 求职职场>
  • 解决方案>
  • 总结汇报>
  • ImageVerifierCode 换一换
    首页 冰点文库 > 资源分类 > DOCX文档下载
    分享到微信 分享到微博 分享到QQ空间

    Java基础教程.docx

    • 资源ID:14543728       资源大小:21.53KB        全文页数:23页
    • 资源格式: DOCX        下载积分:5金币
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录 QQ登录
    二维码
    微信扫一扫登录
    下载资源需要5金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

    加入VIP,免费下载
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Java基础教程.docx

    1、Java基础教程Java基础教程1. 程序结构/最基本的JAVA程序必须有一个类,在这里是class base01class base01 /类的定义是:class 类名 属性和方法/在JAVA中,程序入口也是main函数/main()函数前的限制符顺序必须为public static void,参数必须是/ String类型的数组。以下写法将不被通过public void static main(String args)System.out.println(Hello World!);2. 类与源文件命名/ 类是面向对象程序的处理单元,由(public和private)属性和方法构成。/ 对

    2、象是类的实例化,通过new声明引入。类封装数据和方法、可以继承,/ 方法和算符可以多态重载。类有构造和析构函数,用于初始化和收尾。 / 在同一个JAVA文件中可以定义多个类,但是只有一个类能定义为public/ 而且如果一个类被定义为public,这个文件的文件名必须和该public类/ 的名字相同。对于本类,文件必须命名为base02class A /该类有属性,未定义方法int a;class B /该类有属性,未定义方法int b;public class base02 /该类有一个方法,未定义属性public static void main(String args) System.o

    3、ut.println(Hello World!);3. 数据类型 class code03public static void main(String args) /JAVA的数据类型。请注意大小写/一个字节的整型byte e=3;System.out.println(e);/两个字节的整型short h=4;System.out.println(h);/四个字节的整型int f=4;System.out.println(f);/八个字节的整型long g=5;System.out.println(g);/4个字节的浮点数float a=11.3f;System.out.println(a);

    4、/8个字节的浮点数(更加精确)double b=12.3d;System.out.println(b);/两个字节的单个字符。注意定义字符不能用双引号,只能用单引号char c=s;String ss=xiongyong;System.out.println(c);/bool变量,用于判断真假。注意:true和false不能大写。boolean d=true;System.out.println(d);/final关键字为“常量”,意思是不能再被修改。相当于C+中的constfinal int x=1;/以下这行将被报错,因为x被定义为final,不能被修改。x=x+1;4. 类型转换.0/*

    5、自动类型转换和强制类型转换:不同的数据类型可以进行混合运算。在运算过程中,不同数据类型必须首先转换为相同的数据类型后才能进行运算。各种数据的转换原则是从低级向高级转换。具体如下:char-byte-short-int-long-float-doubleJAVA允许把高级类型转换为低级类型,我们通过强制类型转换实现。语法同C+。*/class code04 void f1()/自动类型转换int nO=2;float fO=2.25f;/以下这行不能被正确编译,因为算出来的结果不可能是整数/int iResult=nO*fO;/正确float fResult=nO*fO;System.out.p

    6、rintln(混合运算结果为:+fResult);void f2()/强制类型转换int nO=2;float fO=2.25f;float iResult=(int)(5.5*fO);System.out.println(强制类型转换结果为:+iResult);public static void main(String args) code03 c3=new code03();c3.f1();c3.f2();5. 运算符 /*+运算符和类型转换+对于数字来说是求和运算。两个short类型数据相加,结果被升级为int类型。+对于字符串来说连接两个字符串的运算*/class code05 vo

    7、id f_add_string()String a=hello ;String b=world;/请观察输出结果,体会字符串+字符串的功能System.out.println( a+b= +a+b);void f_add_number()short x=1,y=2,z;int p;/错误代码。两个short类型数据相加,结果将被升级为int类型。而z却还是short类型z=x+y;/正确代码z=(short)(x+y);System.out.println( 1 + 2 = + z);p= x+y ;System.out.println( 1 + 2 = + p);public static

    8、void main(String args) code04 c4=new code04();c4.f_add_string();c4.f_add_number();6. 数组 /*JAVA允许在程序中调整数组中元素的个数*/class code06 public static void main(String args) int myArray=new int6; /数组用new声明int i;for (i=0;imyArray.length ;i+ ) /数组长度为lengthmyArrayi=i;System.out.println(myArrayi);/重新定义数组,注意,myArry以

    9、前的内容被重置了myArray=new int10;for (i=0; imyArray.length;i+ ) /重新定义数组/myArrayi=i*10;System.out.println(myArrayi);7. 控制:冒泡排序/*数组这是一个常见的“冒泡排序”的程序,用于把几个数按大小进行排序*/class code07 public static void main(String args) /定义一个包含n个数的数组int nArray=11,-5,100,56,321;/JAVA是纯面向对象的语言,对于任何一个数组元素,JAVA都把它看/做一个数组类型的对象,length是这个

    10、类的一个成员变量,它的值等于/该对象中数组元素的个数。所以我们能通过nArray.length获得这个数组/nArray里面包含元素的个数。对于本题,它等于5。int size=nArray.length-1;for (int j=0;jsize;j+) for (int i=0;inArrayi+1) /若逆序则交换数据int tmp=nArrayi;nArrayi=nArrayi+1;nArrayi+1=tmp;for (int i=0;inArray.length ;i+ ) System.out.println(nArrayi+ );8. 控制:显示素数/* 显示1到x之间的所有素数*

    11、/class code08 /判断一个数x是不是素数public boolean is_sushu(int x)for (int i=2;ix ;i+ ) if (x%i=0) return false;return true;/显示1到x之间的所有素数public void disp(int x)for (int i=1;i=x ;i+ ) if (is_sushu(i) System.out.println(i);/程序入口public static void main(String args) code12 c12=new code12();c12.disp(1000);9. 传简单参数

    12、的函数 /* 使用简单数据类型做为参数时的函数传值方式* 当参数为简单数据类型时,参数传值使用复制传值方式,也就是说作为* 参数传进函数的值只是传进来值的一个副本*/class code09 public static void main(String args) System.out.println(Hello World!);int j=10;/本例中calculate函数传递的是简单数据类型参数j,所以使用复制/传递方式,函数中并没有改变原来j的内容calculate(j);System.out.println(=);System.out.println(执行完calculate函数后j

    13、的值为:+j);static void calculate(int j) /静态函数属于类,无需声明对象,可直接调用for (int i=0;i10 ;i+ ) j+;System.out.println(calculate函数中j的值为:+j);10. 传复杂参数的函数 /* 使用复杂数据类型做为参数时的函数传值方式* 当参数为复杂数据类型时,参数传值使用引用传值方式,也就是说作为参数传进函数* 的值只是传进来值的一个别名,直接效果就是函数中对参数做的修改直接就影响到原* 始的值*/class code10public static void main(String args) String

    14、 a=hello;System.out.println(初始阶段数组a的值为: + a);calculate(a);System.out.println(调用calculate函数后,数组a的值为: + a);System.out.println(由此可知,在本函数中,参数是作为复杂变量的别名传进函数的,证据就是在函数中确实改变了函数外部变量a的值);static void calculate(String j)j=world;System.out.println(calculate函数中参数数组j的值为:+j);11. 类与对象 / 类是面向对象程序的处理单元,由(public和privat

    15、e)属性和方法构成。/ 对象是类的实例化,通过new声明引入。类封装数据和方法、可以继承,/ 方法和算符可以多态重载。类有构造和析构函数,用于初始化和收尾。class code11 /基础类A a = new A(); /类的属性,这里还含有初始化int m = 20; /另一个属性public base09() /构造函数,与类同名System.out.println(A.i = + a.i);System.out.println(m = + m);System.out.println(=);change(a);change(m);System.out.println(A.i = + a.i

    16、);System.out.println(m = + m);public static void main(String args) /入口函数new base09(); /实例化一个对象,引发其构造方法执行void change(A aa) /基础类的一个方法(函数)aa.i +=100;void change(int x) /基础类的另一个方法(函数)x+=100;System.out.println(函数内 x = + x);class A / 嵌套的另一个类,恰巧只有属性,没有方法int i = 10;12. 函数返回值/* 函数的返回值* return语句的作用是把一个确定的值返回给

    17、调用它的语句,这个值可以是* 简单变量,也可以是对象。如果返回值是对象,则返回的实际上是对象的* 地址。这个情况和参数传递相同*/class code12/求 x*x+2(y+1)+3的函数static float mathF1(float x,float y)float z=x*x+2*+y+3;/我们可以这样写返回值return z;static float mathF2(float x,float y)/我们也可以这样写返回值(直接返回一个运算表达式的结果)return x*x+2*+y+3;public static void main(String args) /System.out

    18、.println(a);/我们可以这样获得返回值float z=mathF2(4f,5f);System.out.println(运算结果=+z);/我们也可以这样获得返回值System.out.println(运算结果=+mathF1(4f,5f);13. 函数嵌套 /* 函数的嵌套* 函数自己调用自己叫做嵌套*/class code13 static int i=0;/这是一种不好的嵌套,在函数体中无条件的调用了自己。导致的结果就是/无限的循环static void cout_line()System.out.println(=);cout_line();/一种安全的嵌套,有条件的调用自己

    19、,不会导致死循环static void cout_line2()System.out.println(=);i+;if (i10)cout_line2();public static void main(String args) /请注释掉这个函数,因为它将导致死循环,函数永远不结束cout_line();cout_line2();14. 输入输出/* * Java中输入用BufferedReader对象,输出用System.out.println */import java.io.*;class base14 public static void main (String args) Str

    20、ing s; / 创建一个BufferedReader对象从键盘逐行读入数据 InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader(isr); /try/catch是异常处理,catch部分处理try部分的异常(错误) try / 每读入一行后向显示器输出 s = br.readLine(); while (!s.equals() /String的equal方法用于字串比较 System.out.println(Read: + s); s = br.re

    21、adLine(); br.close(); / 关闭输入流 catch (IOException e) / 捕获可能的IOException. e.printStackTrace(); 15. 文件操作/* * Java中用BufferedReader读文件,用PrintWriter写文件*/import java.io.*;public class base15 public static void main (String args) /Part 1: File read String fname = base15.java; File f = new File(fname); try F

    22、ileReader fr = new FileReader(f); BufferedReader br = new BufferedReader(fr); String s = br.readLine(); while ( s != null ) System.out.println(读入: + s); s = br.readLine(); br.close();/ 关闭缓冲读入流及文件读入流的连接. catch (FileNotFoundException e1) System.err.println(File not found: + fname); catch (IOException

    23、e2) e2.printStackTrace(); /Part 2: File write File file = new File(tt.txt); try InputStreamReader isr = new InputStreamReader(System.in); BufferedReader in=new BufferedReader(isr); FileWriter fw = new FileWriter(file); PrintWriter out = new PrintWriter(fw); / 从键盘逐行读入数据输出到文件 String s = in.readLine();

    24、 while (!s.equals() out.println(s); s = in.readLine(); in.close(); / 关闭BufferedReader输入流. out.close(); / 关闭连接文件的PrintWriter输出流. catch (IOException e) System.out.println(e); 16. 综合:Record操作import java.io.*;/* A Driver for testing the Record class* author Ping Feng* version June 2012*/public class bas

    25、e16 public static void main (String args) char choice; Record theRecord; theRecord = new Record (); String s=0; InputStreamReader isr = new InputStreamReader (System.in); BufferedReader br = new BufferedReader(isr); try do System.out.print (n + RecordDriver + n + = + n + 1. Populate the Record + n + 2. Delete the Record + n + 3. Show as


    注意事项

    本文(Java基础教程.docx)为本站会员主动上传,冰点文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰点文库(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2023 冰点文库 网站版权所有

    经营许可证编号:鄂ICP备19020893号-2


    收起
    展开