java 编程-------基本类型

来源:互联网 发布:淘宝会员等级怎么升 编辑:程序博客网 时间:2024/04/30 21:56
 

JAVA基本语法,有好多常见的就不写了,就写上一些特别的:

Java语言中,把字符串作为对象来处理,类StringStringBuffer都可以用来表示一个字符串。(类名都是大写字母打头)

String表示字符串常量

  用String表示字符串:
  String( char chars[ ] );
  String( char chars[ ], int startIndex, int numChars );
  String( byte ascii[ ], int hiByte );
  String( byte ascii[ ], int hiByte, int startIndex, int numChars );
  String使用示例:
  String s=new String() ; 生成一个空串

  下面用不同方法生成字符串"abc":
  char chars1[]={'a','b','c'};
  char chars2[]={'a','b','c','d','e'};
  String s1=new String(chars1);
  String s2=new String(chars2,0,3);
  byte ascii1[]={97,98,99};
  byte ascii2[]={97,98,99,100,101};
  String s3=new String(ascii1,0);
  String s4=new String(ascii2,0,0,3);

 
3.用StringBuffer表示字符串

  StringBuffer( );
/*分配16个字符的缓冲区*/
  StringBuffer( int len );
/*分配len个字符的缓冲区*/
  StringBuffer( String s );
/*除了按照s的大小分配空间外,再分配16个
               字符的缓冲区*/

String中提供了length( )charAt( )indexOf( )lastIndexOf( )getChars( )getBytes( )toCharArray( )等方法。

  
◇ public int length() 此方法返回字符串的字符个数
  ◇ public char charAt(int index) 此方法返回字符串中index位置上的字符,其中index 值的 范围是0~length-1
  ◇ public int indexOf(int ch)
    public lastIndexOf(in ch)
  
  返回字符ch在字符串中出现的第一个和最后一个的位置
  ◇ public int indexOf(String str)
    public int lastIndexOf(String str)
  返回子串str中第一个字符在字符串中出现的第一个和最后一个的位置
  ◇ public int indexOf(int ch,int fromIndex)
    public lastIndexOf(in ch ,int fromIndex)
  返回字符ch在字符串中位置fromIndex以后出现的第一个和最后一个的位置
  ◇ public int indexOf(String str,int fromIndex)
    public int lastIndexOf(String str,int fromIndex)
  返回子串str中的第一个字符在字符串中位置fromIndex后出现的第一个和最后一个的位置。
  ◇ public void getchars(int srcbegin,int end ,char buf[],int dstbegin)
   srcbegin 为要提取的第一个字符在源串中的位置, end为要提取的最后一个字符在源串中的位置,字符数组buf[]存放目的字符串,    dstbegin 为提取的字符串在目的串中的起始位置。
  ◇public void getBytes(int srcBegin, int srcEnd,byte[] dst, int dstBegin)
  参数及用法同上,只是串中的字符均用8位表示。

 
2.类StringBuffer提供了 length( )charAt( )getChars( )capacity()等方法。

  方法capacity()用来得到字符串缓冲区的容量,它与方法length()所返回的值通常是不同的。

 

String类提供的方法:

   concat( )
   replace( )
   substring( )
   toLowerCase( )
   toUpperCase( )

   public String contat(String str);
  用来将当前字符串对象与给定字符串str连接起来。
   public String replace(char oldChar,char newChar);
  用来把串中出现的所有特定字符替换成指定字符以生成新串。
   public String substring(int beginIndex)
  public String substring(int beginIndex,int endIndex);
  用来得到字符串中指定范围内的子串。
   public String toLowerCase();
  把串中所有的字符变成小写。
   public String toUpperCase();
  把串中所有的字符变成大写。

 2StringBuffer类提供的方法:

  append( )
  insert( )
  setCharAt( )

  如果操作后的字符超出已分配的缓冲区,则系统会自动为它分配额外的空间。
   public synchronized StringBuffer append(String str);
  用来在已有字符串末尾添加一个字符串str
   public synchronized StringBuffer insert(int offset, String str);
  用来在字符串的索引offset位置处插入字符串str
   public synchronized void setCharAt(int index,char ch);
  用来设置指定索引index位置的字符值。

  注意:String中对字符串的操作不是对源操作串对象本身进行的,而是对新生成的一个源操作串对象的拷贝进行的,其操作的结果不影响源串。

  相反,StringBuffer中对字符串的连接操作是对源串本身进行的,操作之后源串的值发生了变化,变成连接后的串。

 

还要理解一个短路(Short-Circuit)逻辑运算符,呵呵其实就是&& ,||

JAVA中没有指针的概念但有引用的概念:每个引用占据32位的内存空间,基值指向对象实际所在的内存中的位置,例如:

Data d=new Date();

通常我们称dDate型的对象,实际上d就是引用,它是一个32位的数据,它的值指向该Date对象实际所在的内存空间。


原创粉丝点击