Java面试题一(基础)
1. 如何得到Java应用程序的可用内存?
答:如下代码实现取得总的内存大小和可用内存大小,并打印到控制台上
public class MemoryExp {
public static void main(String[] args) {
System.out.println("Total Memory"+Runtime.getRuntime().totalMemory());
System.out.println("Free Memory"+Runtime.getRuntime().freeMemory());
}
}
2. 如何写一个不需要main方法的java应用程序?
答:可以使用静态代码块来实现一个可以执行但并没有main方法的Java应用程序。如下面的代码是所示:
class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}
3. 请说出上例中的代码为什么可以运行?
答:上面的代码可以运行是因为static代码块会在java类被加载的时候被执行,而且是在main方法被调用之前。在运行时,JVM会在执行静态代码块以后搜索main方法,如果不能找到main方法,就会抛出一个异常,为了避免这个异常,可以使用System.exit(0)来结束应用程序。
4.介绍一下Java的Map以及如何使用HashMap?
Map是一个保存键值对的对象,根据键值可以找到它对应的值。键必须是唯一的,但是值可以重复。HashMap类提供了map接口的主要实现,它使用hash table来实现map接口。这就使得一些常见的操作如get()和put()所用的时间基本不变。
如下代码是一个使用hashmap的实例,它提供了account信息和account balance的对应:
import java.util.*;
public class HashMapDemo {
public static void main( String[] args) {
HashMap hm = new HashMap();
hm.put(“Rohit”, new Double(3434.34));
hm.put(“Mohit”, new Double(123.22));
hm.put(“Ashish”, new Double(1200.34));
hm.put(“Khariwal”, new Double(99.34));
hm.put(“Pankaj”, new Double(-19.34));
Set set = hm.entrySet();
Iterator i = set.iterator();
while(i.hasNext()){
Map.Entry me = (Map.Entry)i.next();
System.out.println(me.getKey() + “ : ” + me.getValue() );
}
//deposit into Rohit’s Account
double balance = ((Do
相关文档:
我今天学习了徐老师讲的ejb3的知识,我做了简单的笔记:
上午讲的是jpa的知识,下午讲的是EJB3的知识:
JPQL语句:hibernate的HQL语句一样。
简单查询:
Query query = em.createQuery("SELECT c from Customer c");
retur ......
66. EJB容器提供的服务
主要提供声明周期管理、代码产生、持续性管理、安全、事务管理、锁和并发行管理等服务。
67. EJB规范规定EJB中禁止的操作有哪些?
1.不能操作线程和线程API(线程API指非线程对象的方法如n ......
java去除字符串中的空格、回车、换行符、制表符
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringUtil {
public static void replaceBlank()
{
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
String str="I am a, I am Hello ok, \n new line ffdsa!";
System.out.p ......
this
对象本身。public class ThisTest {
ThisTest tTest;
public ThisTest(){
tTest = this;
}
public void test(){
System.out.println(this);
}
public static void main(String arg[]){
new ThisTest().test();
}
}
成员方法引用。
成员变量引用。public class ThisTest {
String name ......
类是广泛的概念,表示一个有共同性质的群体。如:人类。
代码:
// 声明一个类“Human”
class Human{
priv ......