java面试题1
来源:互联网 发布:淘宝造物节线上活动 编辑:程序博客网 时间:2024/05/28 23:11
- import java.util.Random;
- /**
- *
- * @author Alexs
- */
- /*
- * Demo1
- */
- class Demo1 {
- public static void main(String[] args) {
- int num=2147483647;
- long temp=num+2L;
- System.out.println(temp);//2147483649
- }
- }
- /*
- * Demo2
- */
- class Demo2{
- public static void main(String[] args) {
- char c='A';
- int num=10;
- switch(c) {
- case 'B':
- num++;
- case 'A':
- num++;
- case 'Y':
- num++;
- break;
- default:
- num--;
- }
- System.out.println(num);//12
- }
- }
- /*
- * Demo3
- */
- class Demo3{
- public static void main(String[] args) {
- int x=10;
- double y=20.2;
- long z=10L;
- String str=""+x+y*z;
- System.out.println(str);//10202.0
- }
- }
- /*
- * Demo4
- */
- class Demo4{
- public static void main(String[] args) {
- int sum=0;
- for(int x=0;x<10;x++) {
- sum+=x;
- if(x%3==0) {
- break;
- }
- }
- System.out.println(sum);//0
- }
- }
- /*
- * Demo5
- */
- class Demo5{
- public static void main(String[] args) {
- System.out.println(inc(10)+inc(8)+inc(-10));//35
- }
- public static int inc(int temp) {
- if(temp>0) {
- return temp*2;
- }
- return -1;
- }
- }
- /*
- * Demo6
- */
- class Demo6{
- public static void main(String[] args) {
- boolean flag=10%2==1 && 10/3==0 && 1/0==0;
- System.out.println(flag?"A":"B");//B
- }
- }
- /*
- * Demo7
- */
- class Demo7{
- public static void main(String[] args) {
- int num=50;
- num=num++*2;
- System.out.println(num);//100
- }
- }
- /*
- * Demo8
- */
- class Demo8{
- public static void main(String[] args) {
- int i=1;
- int j=i++;
- if(i==(++j)&&((i++)==j)) {
- i+=j;
- System.out.println("i="+i);//5
- }
- }
- }
- /*
- * Demo9
- */
- class Demo9{
- public static void main(String[] args) {
- String str="";
- for(int x=0;x<5;x++) {
- str+=x;
- }
- System.out.println(str);//01234
- }
- }
- /*
- * Demo10
- */
- class Demo10{
- public int a() {
- int i=0;
- i++;
- return i;
- }
- public static void main(String[] args) {
- Demo10 d=new Demo10();
- d.a();
- int j=d.a();
- System.out.println(j);//1
- }
- }
- /*
- * Demo11
- */
- class supers{
- String name;
- public supers(){};
- public supers(String name) {
- this.name=name;
- }
- public void fun1() {
- System.out.println("this is class super!"+name);
- }
- }
- class sub extends supers{
- public void fun1() {
- System.out.println("this is class sub!"+name);
- }
- }
- class Demo11{
- public static void main(String[] args) {
- supers s=new sub();
- s.fun1();//this is class sub!null
- }
- }
- /*
- * Demo12
- */
- class Demo12{
- public static void main(String[] args) {
- int i=9;
- switch (i) {
- default:
- System.out.println("default");
- case 0:
- System.out.println("zero");
- case 1:
- System.out.println("one");
- case 2:
- System.out.println("two");
- }
- /*
- * default
- * zero
- * one
- * two
- */
- }
- }
- /*
- * Demo13 写一个单例模式
- */
- class Demo13{
- //饿汉式
- private static final Demo13 d=new Demo13();
- private Demo13() {}
- public static Demo13 getInstance() {
- return d;
- }
- }
- class Demo13Test{
- public static void main(String[] args) {
- Demo13 d1=Demo13.getInstance();
- Demo13 d2=Demo13.getInstance();
- System.out.println(d1==d2);//true
- }
- }
- /*
- * Demo14
- * 生成一个随机密码,密码包含大小写字母及数字,并且可根据输入参数对密码长度进行调整
- */
- class Demo14{
- //定义一下字符串常量
- private static final String CHAR_ALL="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- public String getRandomPwd(int num) {
- String result="";
- //实例化Random对象
- Random r=new Random();
- for(int i=0;i<num;i++) {
- //生成0-62之间的(小于62)int值
- int j=r.nextInt(CHAR_ALL.length());
- //截取下标跟跟int值对应的字符
- String singleChar=CHAR_ALL.substring(j, j+1);
- //拼接
- result+=singleChar;
- }
- return result;
- }
- public static void main(String[] args) {
- Demo14 d=new Demo14();
- System.out.println(d.getRandomPwd(6));
- }
- }
- /*
- * Demo15
- */
- class Demo15 {
- public static void main(String []args) {
- int i=9;
- int j=9;
- //System.out.println(i==j);//true
- //System.out.println(j==i++);//true
- //System.out.println(j==++i);//false
- //System.out.println(j++==i);//true
- //System.out.println(++j==i);//false
- }
- }
阅读全文
0 0
- java面试题1
- JAVA面试题-1
- java面试题1
- java面试题1
- JAVa面试题1
- java面试题1
- java面试题1
- java面试题1
- java面试题1
- java面试题1
- java面试题1
- java面试题1
- java面试题1
- Java面试题1
- JAVA面试题1
- JAVA面试题【1】
- Java面试题1
- java面试题1
- 魔咒字典
- Java9中的GC 调优
- 原HP大中华区总裁孙振耀的退休感言
- Node.js--Mongoose使用进阶
- 我们逃离北上广,美国人逃离硅谷
- java面试题1
- IOS 获取图片的size
- CV研究组
- accept获取对端IP地址,getsockname获取本端IP地址
- ZooKeeper搭建
- [Leetcode] 498. Diagonal Traverse 解题报告
- Linux常见日志文件
- 微信小程序学习资料
- C++学习资料