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.
How do you assign value to variables using one declaration? BTW، this is called initializations.
When declaring two or more variables of the same type using a comma-separated list، you can give one or more of those variables an initial value. For example: int a، b = 8، c = 19، d; // b and c have initializations
When are scopes created and what do they determine?
A block defines a scope. Thus، each time you start a new block، you are creating a new scope. A scope determines what objects are visible to other parts of your program. It also determines the lifetime of those objects.
What is included in a method’s scope? What is a method body? How do scopes provide encapsulation?
The scope defined by a method begins with its opening curly brace. However، if that method has parameters، they too are included within the method’s scope. A method’s scope ends with its closing curly brace. This block of code is called the method body. As a general rule، variables declared inside a scope are not visible (that is، accessible) to code that is defined outside that scope. Thus، when you declare a variable within a scope، you are localizing that variable and protecting it from unauthorized access and/or modification. Indeed، the scope rules provide the foundation for encapsulation.
What is a local variable?
A variable declared within a block is called a local variable.
Why is it a bad idea to initialize a variable at the end of a block?
Within a block، variables can be declared at any point، but are valid only after they are declared. Thus، if you define a variable at the start of a method، it is available to all of the code within that method. Conversely، if you declare a variable at the end of a block، it is effectively useless، because no code will have access to it.
What happens to a variable that is initialized in a loop block?
If a variable declaration includes an initializer، that variable will be reinitialized each time the block in which it is declared is entered.
What are operators and what are the four general classes of operators?
Java provides a rich operator environment. An operator is a symbol that tells the compiler to perform a specific mathematical or logical manipulation. Java has four general classes of operators: arithmetic، bitwise، relational، and logical. Java also defines some additional operators that handle certain special situations. Operator : Meaning + : Addition - : Subtraction * : Multiplication / : Division % : Modulus ++ : Increment – : Decrement
When the division of an integer would result in 3.999، what would happen to the decimal numbers?
The new interger value would be 3. When division is applied to an integer، any remainder will be truncated. For example 3/2 would be 1.
What does the % operator do?
10 % 3 is 1. In Java، the % (Modulus) can be applied to both integer and floating-point types. Thus، 10.0 % 3.0 is also 1. It yields the remainder of an integer division.
What side of a variable do the in/decrement operators need to be on?
”++x” or “x++”. Both the increment and decrement operators can either precede (prefix) or follow (postfix) the operand. When an increment or decrement operator precedes its operand، Java will perform the corresponding operation prior to obtaining the operand’s value for use by the rest of the expression. If the operator follows its operand، Java will obtain the operand’s value before incrementing or decrementing it.
How are the terms relational operators different from logical operators? What are the operators associated with both?
In the terms relational operator and logical operator، relational refers to the relationships that values can have with one another، and logical refers to the ways in which true and false values can be connected together. Since the relational operators produce true or false results، they often work with the logical operators.
How is an exclusive or statement different from a regular or statement?
the outcome of an exclusive OR operation is true when exactly one and only one operand is true. “true and false” or “false and true”
What’s the difference between a normal and short-circuit operands?
The only difference between the normal and short-circuit versions is that the normal operands will always evaluate each operand، but short-circuit versions will evaluate the second operand only when necessary.
When is a short-circuit and operator useful?
To prevent a divide-by-zero، the if statement first checks to see if d is equal to zero. If it is، the short-circuit AND stops at that point and does not perform the modulus division.
What is the formal name for short-circuit operators?
The formal specification for Java refers to the short-circuit operators as the conditional-or and the conditional-and operators، but the term “short-circuit” is commonly used.
How can you assign multiple variables to one value in a single line of code?
The assignment operator does have one interesting attribute that you may not be familiar with: it allows you to create a chain of assignments. For example، consider this fragment: int x، y، z; x = y = z = 100; // set x، y، and z to 100
When is it more beneficial to use the non-short-circuit version of an operands?
if your code expects the right-hand operand of an AND or OR operation to be evaluated، you must use Java’s non-short-circuit forms of these operations.
What are the shorthand logical and arithmetic expressions you can use in Java?
55This shorthand will work for all the binary operators in Java (that is، those that require two operands). The general form of the shorthand is var op = expression; Thus، the arithmetic and logical shorthand assignment operators are the following: += and -= are some examples.
What is the formal name of the shorthand operation like x += 10; and x -= 10;
Because these operators combine an operation with an assignment، they are formally referred to as compound assignment operators.
When will an automatic type conversion occur?
When one type of data is assigned to another type of variable، an automatic type conversion will take place if ● The two types are compatible. ● The destination type is larger than the source type.
What are widening conversions?
For example، the int type is always large enough to hold all valid byte values، and both int and byte are integer types، so an automatic conversion from byte to int can be applied. For widening conversions، the numeric types، including integer and floating-point types، are compatible with each other.
When do widening conversions fail?
Although there is an automatic conversion from long to double، there is no automatic conversion from double to long، since this is not a widening conversion.
What are cast conversions، when are they useful، and what I their general form?
Although the automatic type conversions are helpful، they will not fulfill all programming needs because they apply only to widening conversions between compatible types. For all other cases you must employ a cast. A cast is an instruction to the compiler to convert one type into another. Thus، it requests an explicit type conversion. A cast has this general form: (target-type) expression Here، target-type specifies the desired type to convert the specified expression to. Example: double x، y; // … (int) (x / y): The parentheses surrounding x / y are necessary. Otherwise، the cast to int would apply only to the x and not to the outcome of the division.
What happens to the value in a narrowing conversion?
When a cast involves a narrowing conversion، information might be lost. For example، when casting a long into a short، information will be lost if the long’s value is greater than the range of a short because its high-order bits are removed.
What is the input method complementary to the System.out method? What type of value is returned?
System.in is the complement to System.out. It is the input object attached to the keyboard. The read( ) method Page 67waits until the user presses a key and then returns the result. The character is returned as an integer، so it must be cast into a char to assign it to a char variable.
What does it mean to have input as line buffered?
console input is line buffered. Here، the term buffer refers to a small portion of memory that is used to hold the characters before they are read by your program. In this case، the buffer holds a complete line of text. As a result، you must press ENTER before any character that you type will be sent to your program.
What clause must be stated in the main() when using System.in.read?
Because System.in.read( ) is being used، the program must specify the throws java.io.IOException clause. This line is necessary to handle input errors. It is part of Java’s exception handling mechanism
What is the complete form an if statement?
The complete form of the if statement is if(condition) statement; else statement; where the targets of the if and else are single statements. The else clause is optional.