下载安卓APP箭头
箭头给我发消息

客服QQ:3315713922

asp程序员如何转行为J2EE之java基础上(7)

作者:课课家教育     来源: http://www.kokojia.com点击数:723发布时间: 2016-01-29 18:33:52

标签: java开发java语言java应用

大神带你学编程,欢迎选课

  Visual Basic 函数和子过程与 java 方法的比较

  Visual Basic 允许定义函数和子过程。函数和子过程的主要区别是子过程不返回值,而函数返回值。在 Java 技术中,函数被称为 方法。Java 语言没有与 Visual Basic 中的子过程等价的用法。但是,在 Java 语言中,可以使用关键字 void 定义不返回值的方法,这种方法大致等价于子过程。只能将 Java 方法声明位某个类的成员;不能在 Java 类之外定义方法。在清单 3 展示的例子中,一个 Java 方法返回值而另一个不返回值。

asp程序员如何转行为J2EE之java基础上(7)_java开发_java语言_课课家

  清单 3. Java 方法的返回类型

  public class MyClass {

  // This method doesn't return a value

  public void myMethod1(String arg) {

  ...

  }

  // This method returns an integer

  public int myMethod2(String arg) {

  int i;

  ...

  return i ;

  }

  数组

  在 Java 语言中,数组是具有属性的对象,其中最重要的是 长度 属性,您可以使用该属性确定数组的大小。Java 数组的索引值始终从 0 开始,数组的声明大小包括第 0 个元素。因此,大小为 100 的数组意味着有效索引是从 0 到 99。另外,您还可以将用于表示数组的方括号([ ])与数组类型而非数组名绑定起来。Java 语言允许采用数组字面值,这样可以将数组初始化为一组预定义的值。清单 4 展示了一些例子。

  清单 4. 数组

  Visual Basic           Java

  'An array with 100 integers      // An array of 100 integers

  Dim a(99) As Integer         int[] a = new int[100];

  'An array of Strings initialized   // An array of Strings initialized

  b = Array("Tom","Dick", "Harry")   String[] b = {"Tom","Dick", "Harry"};

  'Iterating through an array      // Iterating through an array of length 100

  ' of length 100            int [] c = new int [100];

  Dim c(99) As Integer         for (int i = 0; i <.length; i++) {

  For i=0 To UBound(c)           c[i] = i;

  c(i) = i              }

  Next

  字符串

  Visual Basic 使用 String 数据类型来表示字符串。您可通过 String 类的对象来表示 Java 字符串。Visual Basic 和 Java 字符串字面值由一系列加引号的字符表示。在 Java 语言中,可以采用两种方法创建 String 对象:使用字符串字面量,或使用 构造函数。 String 对象是固定的,这意味着在赋予某个 String 一个初始值后,就不能改变它。换句话说,如果您要更改 String 引用的值,则需要将一个新 String 对象赋值给该引用。由于 Java 字符串是对象,所以可以通过 String 类定义的 接口与这些字符串进行交互。(您可以在本文稍后的 面向对象编程简介 中了解更多的有关构造函数和接口的信息。) String 类型包含很多接口以及不少有用的方法。

  清单 5 演示了一些最常用的方法。请尝试编译并运行该例子。记住将源文件命名为 StringTest.java,并且不要忘记文件名的大小写很重要。

  清单 5. Java 语言中的字符串

  /*

  * The StringTest class simply demonstrates

  * how Java Strings are created and how

  * String methods can be used to create

  * new String objects. Notice that when you

  * call a String method like toUpperCase()

  * the original String is not modified. To

  * actually change the value of the original

  * String, you have to assign the new

  * String back to the original reference.

  */

  public class StringTest {

  public static void main(String[] args) {

  String str1 = "Hi there";

  String str2 = new String("Hi there");

  // Display true if str1 and str2 have the value

  System.out.println(str1.equals(str2));

  // A new uppercase version of str1

  System.out.println(str1.toUpperCase());

  // A new lowercase version of str2

  System.out.println(str1.toLowerCase());

  System.out.println(str1.substring(1,4));

  // A new version of str1 w/o any trailing whitespace chars

  System.out.println(str1.trim());

  // Display true if str1 start with "Hi"

  System.out.println(str1.startsWith("Hi"));

  // Display true if str1 ends with "there"

  System.out.println(str1.endsWith("there"));

  // Replace all i's with o's

  System.out.println(str1.replace('i', 'o'));

  }

  }

赞(0)
踩(0)
分享到:
华为认证网络工程师 HCIE直播课视频教程