Java Magazine, Sept/Oct 2017
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2017 78 fix this in subparagraphs under section 310 that this happens for values that fall in the range of 128 through 127 and for Boolean values In light of the preceding paragraphs its clear that the two comparison expressions both result in the value true Because Java allows any data type to be concatenated with a String using the operator the efect is that the resulting output is the word true followed by a space which is followed by a second occurrence of the word true Therefore option A is correct and options B C and D are incorrect The possibility of a compilation error might be suspected in the later lines because a primitive is being assigned to an object reference Its certainly true that Java will not permit an object reference even one of Integer type to refer to a primitive value and in a general case such code would be suspect But of course because the boxing feature was added a long time ago in Java 5 the necessary conversion happens naturally and the two assignments that follow line n1 are entirely correct therefore no error occurs Thus option E is incorrect Question 2 The correct answer is option C This question investigates the nature of a static method and what diferentiates it from an instance method Suppose this method were added to the sample code in the question public static void doStuff y 99 This code would fail to compile due to complaints about accessing the variable y Interestingly the IDE that I prefer for Java development makes the following imprecise and misleading statement non static variable y cannot be accessed from a static context Its also pretty common to hear casual observations such as you cant access instance fields from static methods However the command line compiler in Oracles JDK 8 makes a subtly but critically diferent statement It reports non static variable this cannot be referenced from a static context That comment hits the nail on the head The implicit reference this exists only in a non static
You must have JavaScript enabled to view digital editions.