Java data type Byte, Char__java

Source: Internet
Author: User
Java has 8 basic data types, namely byte, int, long, char, float, double, Boolean. a brief introduction to 1.byte and Char

Sometimes it's always confusing byte and char, so just do it right now:

A byte integer occupies 8 bits in memory, which is a byte. Number of tables:-128--127. (character type char 2 bytes).

There are three ways to represent integer constants in Java:

Where the 8-in integer constant begins with 0. 16-based integers begin with 0x or 0X, and 10-15 are a-f at the beginning (the a--f is case-insensitive).

Character type

Character types are typically used to represent a single byte, and the character constants must be enclosed in single quotes (""). The Java language uses a 16-bit Unicode encoding set as the encoding, which supports characters in various languages.

There are three representations of the number of constants: a single character is used to specify the amount of constants: for example, ' a ', ' a ', ' 8 ', and so on. Represents a specific constants of characters by means of an escape character. For example: ' \ n ', ' \ t ', and so on. Unicode values are directly applied to denote the amount of character constants. The format is: ' \uxxxx ', where xxxx represents a 16 integer.

Character constants can also be expressed in 16-encoded notation, with the range: ' \u0000 '----' \uffff ', which can represent 65,536 characters, with the first 256 characters (' \u0000 '---' \u00ff ') and the characters in the ASCII code exactly coincident.

Not only that, a character's value can also be used as an integer, but it is an unsigned integer, which is all integers, but he is a 16-bit unsigned integer with a range of 0----65535.

If you assign an int integer of 0---65535 to a variable of type char, the system automatically treats the int integer as a char type.

The following is a simple example:

public class Testchar {public

	static void Main (string[] args) {
		//directly specify a single character as the word constants quantity
		char achar= ' a ';
        Use the escape character as the constants amount of the word.
		Char enterchar= ' \ r ';
		Use Unicode encoding values as the character constants
		Char ch= ' \u9999 ';
		SYSTEM.OUT.PRINTLN (CH);
		Defines a medium character constants
		char zhong= ' Medium ';
		Use int zhongvalue=zhong directly as an int variable type as a char variable
		;
		System.out.println (zhongvalue);
		Assigns a 0--65535 int integer directly to a char variable
		char c=97;
		System.out.println (c);
	}
}

2. Escape characters


Single quotes, double quotes, and backslashes in Java have special uses, and you should use an escape character representation.

For example, we want to represent an absolute path in a Java program: "C:\codes, but this type of writing does not get the result of our hope of a backslash, because Java will take the backslash as an escape character, so we should write the following form:" C:\\codes ", only two backslashes at the same time, Java will take the first backslash as the escape character, and the last backslash to form a real slash.

Common escape characters in Java:

\b Backspace

\ n Line Feed

\ r return character

\ t tab

\ "Double Quotes

\ ' Single quotes

\ reverse Slash

3. Force type Conversions

The following program demonstrates an instance of coercion type conversion

public class Narrowconversion {public
	static void Main (string[] args) {
		int ivalue=233;
		Forces the conversion of a value of type int to byte
		   bvalue= (Byte) ivalue;
		   Will output -23
		   System.out.println (bvalue);
		   
		     Double dvalue=3.98;
		   Forces the value of a double to be converted to int
		     int i= (int) dvalue;
		     System.out.println (i);
	}
}

The output after the run:

-23
3

In the above program, a 233 integer coercion type is converted to a byte type, which becomes 23, which is the typical overflow.

Now for the results of the above analysis:

First, let's take a look at the basics of a computer:

All numbers in the bottom of the computer are in the form of 2, the original code is to directly convert a 10 to 2 number, but the computer is in the form of complement to save all the integers . The calculation rules of the complement are as follows: the complement of the positive number and the original code are identical, the complement of the negative number is its inverse code plus 1; the inverse code is to reverse the original code by bit, except the highest bit (symbol bit) remains unchanged.

Now go ahead and take a look at the transition, and here's a demonstration of the conversion process:

32-bit INT-type 00000000000000000000000011101001
//Convert to 8-bit byte 11101001



Above, is the negative complement and the source code conversion schematic diagram.

The following program randomly generates a 6-bit string.

public class Randomstr {public

	static void Main (string[] args) {
		
		//define an empty string of strings
		result= "";
		Perform 6 cycles for
		(int i=0;i<6;i++) {
			//Generate a 97--122 int integer
			 int intval= (int) (Math.random () *26+97);
			 cast int to char and connect to the
			 result=result+ (char) intval
		after result Random output string
		System.out.println (result);
		System.out.println ((char));
		System.out.println ((char) 122);
	}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.