You can view any sample code on any topic in this book and map the braces. It is time to give two more examples. Do you think it`s legal? You don`t need to switch between exam numbering systems. You must recognize valid literal values that can be assigned to numbers. The variable is initialized to 10. However, because x is not initialized before being used in the expression in line 7, the compiler generates the following error: The first step in determining the range is to identify the code blocks. In this case, there are three blocks. You may recognize it because there are three sets of braces. Starting from the innermost set, we can see where the block of the while loop begins and ends. Repeat this while we exit for the if instruction block and method block. Table 2.4 shows the line numbers with which each block begins and ends. There are only four rules to follow for legal identifiers: If garbage collection is not handled properly, it can lead to catastrophic performance and security issues, the most common of which is that an application is low on memory.
However, another similar problem is when secure data such as a credit card number remains in memory long after use and can be read by other programs. Luckily, Java handles many of these complex issues for you. There are important differences you should be aware of between primitives and reference types. First, reference types can be set to null, which means that they do not currently refer to an object. Primitive types generate a compiler error when you try to set them to null. In this example, the value cannot point to null because it is int: The last thing you need to know about numeric literals is that you can have underscores in numbers to make them easier to read: before you can use a variable, it needs a value. Some types of variables set this value automatically, while others require the programmer to specify it. In the following sections, we discuss the differences between the default values for local, instance, and class variables. 1.Which of the Java primitives are Java primitives? (Select all that apply) A.
int B. lang C. short D. Boolean E. String You learned that local variables are declared in a method. How many local variables do you see in this example? On the other hand, a class variable is a variable defined at the class level and shared by all instances of the class. It can even be publicly accessible to classes outside the class without having to use an instance. In our previous Person example, a shared class variable could be used to represent the list of people in today`s zoo. You can tell that a variable is a class variable because it is preceded by the static keyword.
You can find out in Chapter 7. Right now, all you know is that a variable is a class variable if it has the static keyword in its declaration. If you are writing code that initializes fields in multiple places, you must follow the order in which they were initialized. It is simply the order in which different methods, constructors, or blocks are called when an instance of the class is created. We will add some additional rules to the initialization order in Chapter 8. In the meantime, you have to remember: the compiler is also smart enough to detect more complex initializations. In this example, there are two branches of code: B is valid because an underscore is a valid identifier. How many blocks do you see in the following example? How many instance initializers do you see? Don`t worry, you don`t need to memorize the entire list of reserved words. The exam only asks you for those that are commonly used, such as class and for. Table 2.2 lists all reserved words in Java. For verification, you should be aware that System.gc() is not guaranteed to work or do anything, and you should be able to detect when objects are suitable for garbage collection.
The response is initialized in both branches of the if statement, so the compiler is completely satisfied. He knows that whether the verification is true or false, the Response to Something value is set before it is used. The otherAnswer variable is initialized but never used, but the compiler is just as happy. Keep in mind that the compiler is only affected if you try to use uninitialized local variables. It does nothing against those you never use. There are a few other things you should know about digital primitives. If a number is present in the code, it is called literal. By default, Java assumes that you set an int value with a numeric literal. In the following example, the number shown is greater than what fits in an int. Keep in mind that you are not supposed to remember the maximum value of an int. The exam will include him in the question when he introduces himself. Garbage collection refers to the process of automatically freeing up memory on the heap by deleting objects that are no longer accessible in your program.
There are many different algorithms for garbage collection, but you don`t need to know any for the exam. However, if you`re curious, one algorithm is to keep a counter on how many places an object is accessible at any given time and question it for garbage collector if the counter reaches zero. Because bitesOfCheese is declared in an if instruction block, the scope is limited to that block. When the compiler gets to line 7, he complains that he doesn`t know anything about this bitesOfCheese thing and gives an error: this is where it gets tricky.