import java.util.Stack;
public class Stacks {
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack person= new Stack();
person.push("adam");
person.push("brian");
person.push("charlie");
System.out.println("top person"+person.peek());
}
}
import java.util.Stack;
Does java.util.* needs to be imported all the time?please advise
Does not jdk 7 or 8 bundled and come with util package. which packages come by default with jdk.
java.util.Stack stack = new java.util.Stack();
Package directives and import statements are syntatical elements -- they do not tell the JVM where to find the compiled bytecode of a class at runtime. ClassLoaders do this based on classpath definitions which specify jars , folders, etc. where dependent classes might be found.
java.util.Stack stack = new java.util.Stack();
Jim: and organize (sort) them based on configuration rules you provide.
Author: please elaborate on how to provide above configuration. i do not remember providing it.
Reply: In eclipse, click on the Window->Preferences menu item.In the text box on the top left of the dialog type organize imports and hit ENTER or click thru the tree to Java->Code Style->Organize Imports. This dialog is where you set the configuration for how eclipse reponds when you use the organize imports feature.
public class TestHashMap {
public static void main(String[] args) {
HashMap hashMap = new HashMap();
Dog d1 = new Dog("red");
Dog d2 = new Dog("black");
Dog d3 = new Dog("white");
Dog d4 = new Dog("white");
hashMap.put(d1, 10);
hashMap.put(d2, 15);
hashMap.put(d3, 5);
hashMap.put(d4, 20);
//print size
System.out.println(hashMap.size());
//loop HashMap
for (Entry entry : hashMap) {
System.out.println(entry.getKey().toString() + " - " + entry.getValue());
}
}
}
Jim: I think the Eclipse default might be 10.
Author: where to see it in eclipse and how to change it. I did not understand what it means.
Repy: This is in the Organize Imports configuration as discussed in the first question.
The java.lang package contains classes that are fundamental to the Java language, many (perhaps all) are used directly by the compiler. For this reason, the java.lang package is implicitly imported by the compiler so you can simply refer to String and not have to type java.lang.String everywhere you want to reference that class.
r you could import java.util.* to define how to resolve all the classes and interfaces in that package. So while not technically required, it is good practice to use import statements to specify the fully-qualified names of classes on which your code is depending because it leads to more readable code and the imports serve to document your dependencies.
Package directives and import statements are syntatical elements -- they do not tell the JVM where to find the compiled bytecode of a class at runtime. ClassLoaders do this based on classpath definitions which specify jars , folders, etc. where dependent classes might be found
top personcharlie