Lesson 2: Data Types and Program Control Flashcards
hat are the two general categories of built-in data types?
Java contains two general categories of built-in data types: object-oriented and non-object-oriented. Java’s object-oriented types are defined by classes،
What are the eight primitive types of data، do we use primitive types?
at the core of Java are eight primitive (also called elemental or simple) types of data، which are shown in Table 2-1. The term primitive is used here to indicate that these types are not objects in an object-oriented sense، but rather، normal binary values. These primitive types are not objects because of efficiency concerns. boolean: Represents ture/false values byte: 8-bit integer char: Character double: Double-precision floating point float: Single-precision floating point int: Integer long: Long integer short: Short integer
What are the 4 integer data types in Java? What range of numbers do they support?
Java defines four integer types: byte، short، int، and long. Type : Width in Bits : Range byte : 8 : ±128 short : 16 : ±32،768 int : 32 : 2،147،483،648(2billion147million) long : 64 : ±9،223،372،036،854،775،808(9quintillion223quadrillion)
What are unsigned integers and does Java support them?
Java does not support unsigned (positive-only) integers.
Where are byte variables especially useful?
Variables of type byte are especially useful when working with raw binary data that may not be directly compatible with Java’s other built-in types.
If char is formally specified as an integral type، why isn’t it used for number values?
The formal specification for Java defines a type category called integral types، which includes byte، short، int، long، and char. They are called integral types because they all hold whole-number، binary values. However، the purpose of the first four is to represent numeric integer quantities. The purpose of char is to represent characters. Therefore، the principal uses of char and the principal uses of the other integral types are fundamentally different.
How many bits wide are float and double types? Which type does the sqrt() method use?
Type float is 32 bits wide and type double is 64 bits wide. Of the two، double is the most commonly used، and many of the math functions in Java’s class library use double values. For example، the sqrt( ) method (which is defined by the standard Math class) returns a double value that is the square root of its double argument.
If characters in other code languages have 8 bits، how many bits is Java’s Unicode.
Java uses Unicode. Unicode defines a character set that can represent all of the characters found in all human languages. In Java، char is an unsigned 16-bit type having a range of 0 to 65،535.
How do you assign a character variable?
A character variable can be assigned a value by enclosing the character in single quotes. char ch; ch = ‘x’;
Why does java use unicode instead of just using English letters?
Java was designed for worldwide use. Thus، it needs to use a character set that can represent all the world’s languages. Unicode is the standard character set designed expressly for this purpose. Of course، the use of Unicode is inefficient for languages such as English، German، Spanish، or French، whose characters can be contained within 8 bits.
What is returned when a relational operator comes up as true and when it comes up as false?
the outcome of a relational operator، such as <، is a boolean value. This is why the expression 10 > 9 displays the value “true.”
What are literals in Java?
In Java، literals refer to fixed values that are represented in their human-readable form. For example، the number 100 is a literal. Literals are also commonly called constants.
What types of data can literals be?
Java literals can be of any of the primitive data types. The way each literal is represented depends upon its type. Integer literals are specified as numbers without fractional components. For example، 10 and –100 are integer literals. Floating-point literals require the use of the decimal point followed by the number’s fractional component. For example، 11.123 is a floating-point literal.
How do you specify a long literal and a float literal.
By default، integer literals are of type int. If you want to specify a long literal، append an l or an L. For example، 12 is an int، but 12L is a long. By default، floating-point literals are of type double. To specify a float literal، append an F or f to the constant. For example، 10.19F is of type float.
How can you separate digits (numerical literals) in Java to make larger numbers more readable?
You can embed one or more underscores into an integer or floating-point literal. Doing so can make it easier to read values consisting of many digits. When the literal is compiled، the underscores are simply discarded.
What is the octal number system?
The number system based on 8 is called octal، and it uses the digits 0 through 7. In octal the number 10 is the same as 8 in decimal.
What is the hexadecimal number system?
The base 16 number system is called hexadecimal and uses the digits 0 through 9 plus the letters A through F، which stand for 10، 11، 12، 13، 14، and 15. For example، the hexadecimal number 10 is 16 in decimal.
In Java، what do you have to put in front of an octal and hexadecimal numbering system?
A hexadecimal literal must begin with 0x or 0X (a zero followed by an x or X). An octal literal begins with a zero. hex = 0xFF; // 255 in decimal oct = 011: // 9 in decimal
How do you indicate a binary literal in Java?
It is possible to specify an integer literal by use of binary. To do so، precede the binary number with a 0b or 0B.
What are the escape sequence characters? Which ones do what?
Java provides special escape sequences، sometimes referred to as backslash character constants: Escape Sequence : Description ' : Single quote " : Double quote \ : Backslash \r : Carriage return \n : New line \f : Form feed \t : Horizontal tab \b : Backspace \ddd : Octal constant (where ddd is an octal constant) \uxxxx : Hexideciam constant (Where xxxx is a hex constant)
What is a string in Java?
A string is a set of characters enclosed by double quotes
How are strings different than a character literals?
You must not confuse strings with characters. A character literal represents a single letter of type char. A string containing only one letter is still a string. Although strings consist of characters، they are not the same type.
How do you change the type of data a variable can hold?
You cant. Declare a variable of any valid type، including the simple types just described، and every variable will have a type. Thus، the capabilities of a variable are determined by its type. For example، a variable of type boolean cannot be used to store floating-point values. Furthermore، the type of a variable cannot change during its lifetime.
When must you give a variable a value?
In general، you must give a variable a value prior to using it.