A: Definitions and Short Answers i. List the eight primitive data types in Java.
ID: 3588324 • Letter: A
Question
A: Definitions and Short Answers i. List the eight primitive data types in Java. ii. List the non-access modifiers we have seen in class. Describe what each are used for iii. Briefly describe the entire process of writing a Java program to running in it from the command line. (Be sure to explain what javac and java are and what they do.) iv. In a Java program, why does main need to be public? v. In a Java program, the main method is static. What would be the consequences if vi. Suppose you have a final array of numbers as an attribute. Explain why you can or vii. Give a concise definition of a method signature. What is the method signature of the Java did not require this? (i.e., how would you run a program?) cannot change the numbers in the array. main method of any program in Java? viii. What is the difference between a primitive data type and a reference data type in Java? ix. Briefly describe the difference between a class attribute and an instance attribute x. Briefly describe the difference between a class method and an instance method. xi. In the statement System.out.println ("hello, world!");, explain what System, out and println each are xii. Briefly describe the difference between the stack and the heap in our memory model xiii. Briefly describe the four sections of the memory model discussed in class: stack, heap, data segment, code segment xiv. What is "this" in Java? xv. Briefly describe the two uses of this in a constructor. xvi. Briefly describe what an activation record is and what it is used for. xvii. Java provides eight primitive wrapper classes. Explain what they are xviii. Strings are immutable in Java. What does this mean? xix. Can a Java class have no constructors? (Explain yes or no.) xx, Given String s = new String ( "cat"); and String t = "cat", draw the box & arrow diagram for these two variablesExplanation / Answer
1)Answer:
The eight primitive data types in Java are:
2)Answer:
Java provides a number of non-access modifiers to achieve many other functionalities.
The static modifier for creating class methods and variables.
The final modifier for finalizing the implementations of classes, methods, and variables.
The abstract modifier for creating abstract classes and methods.
The synchronized and volatile modifiers, which are used for threads
3)Answer:
Writing a Java hello world program
Open a simple text editor program such as Notepad and type the following content:
1
2
3
4
5
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Save the file as HelloWorld.java (note that the extension is .java) under a directory, let’s say, C:Java.
Every Java program starts from the main() method. This program simply prints “Hello world” to screen.
Compiling it
Now let’s compile our first program in the HelloWorld.java file using javac tool. Type the following command to change the current directory to the one where the source file is stored:
cd C:Java
And type the following command:
javac HelloWorld.java
That invokes the Java compiler to compile code in the HelloWorld.java file into bytecode. Note that the file name ends with .java extension.
If everything is fine (e.g. no error), the Java compiler quits silently, no fuss. After compiling, it generates the HelloWorld.class file which is bytecode form of the HelloWorld.java file. Now try to type dir in the command line, we’ll see the .class file
So remember a Java program will be compiled into bytecode form (.class file).
Running it
It’s now ready to run our first Java program. Type the following command:
java HelloWorld
That invokes the Java Virtual Machine to run the program called HelloWorld (note that there is no .java or .class extension)
4)Answer:
Java specifies several access modifiers e.g. private, protected and public. Any method or variable which is declared public in Java can be accessible from outside of that class. Since the main method is public in Java, JVM can easily access and execute it.
The main method must be declared public, static and void in Java otherwise, JVM will not able to run Java program.
2. JVM throws NoSuchMethodException:main if it doesn't find the main method of predefined signature in class which is provided to Java command. E.g. if you run java Helloworld than JVM will search for public static void main String args[]) method in HelloWorld.class file.
3. The main method is an entry point for any Core Java program. Execution starts from the main method.
4. The main method is run by a special thread called "main" thread in Java. Your Java program will be running until your main thread is running or any non-daemon thread spawned from the main method is running.
5. When you see "Exception in Thread main” e.g.
Exception in Thread main: Java.lang.NullPointerException it means Exception is thrown inside main thread.
6. You can declare the main method using varargs syntax from Java 1.5 onwards e.g.
public static void main(String... args)
7. Apart from static, void and public, you can use a final, synchronized and strictfp modifier in the signature of the main method in Java.
8. The main method in Java can be overloaded like any other method in Java but JVM will only call the main method with specified signature specified above.
1
2
3
4
5
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.