javaifajlx的简单介绍

博主:adminadmin 2022-12-24 08:21:06 70

本篇文章给大家谈谈javaifajlx,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java试卷

第一,谈谈final,

finally,

finalize的区别。

final

修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承。因此一个类不能既被声明为

abstract的,又被声明为final的。将变量或方法声明为final,可以保证它们在使用中不被改变。被声明为final的变量必须在声明时给定初值,而在以后的引用中只能读取,不可修改。被声明为final的方法也同样只能使用,不能重载

finally

再异常处理时提供

finally

块来执行任何清除操作。如果抛出一个异常,那么相匹配的

catch

子句就会执行,然后控制就会进入

finally

块(如果有的话)。

finalize?方法名。java

技术允许使用

finalize()

方法在垃圾收集器将对象从内存中清除出去之前做必要的清理工作。这个方法是由垃圾收集器在确定这个对象没有被引用时对这个对象调用的。它是在

object

类中定义的,因此所有的类都继承了它。子类覆盖

finalize()

方法以整理系统资源或者执行其他清理工作。finalize()

方法是在垃圾收集器删除对象之前对这个对象调用的。

第二,anonymous

inner

class

(匿名内部类)

是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?

匿名的内部类是没有名字的内部类。不能extends(继承)

其它类,但一个内部类可以作为一个接口,由另一个内部类实现。

第三,static

nested

class

inner

class的不同,说得越多越好(面试题有的很笼统)。

nested

class

(一般是c++的说法),inner

class

(一般是java的说法)。java内部类与c++嵌套类最大的不同就在于是否有指向外部的引用上。具体可见http:

//

;page=1

注:

静态内部类(inner

class)意味着1创建一个static内部类的对象,不需要一个外部类对象,2不能从一个static内部类的一个对象访问一个外部类对象

第四,和的区别。

是位运算符。是布尔逻辑运算符。

第五,hashmap和hashtable的区别。

都属于map接口的类,实现了将惟一键映射到特定的值上。

hashmap

类没有分类或者排序。它允许一个

null

键和多个

null

值。

hashtable

类似于

hashmap,但是不允许

null

键和

null

值。它也比

hashmap

慢,因为它是同步的。

第六,collection

collections的区别。

collections是个java.util下的类,它包含有各种有关集合操作的静态方法。

collection是个java.util下的接口,它是各种集合结构的父接口。

第七,什么时候用assert。

断言是一个包含布尔表达式的语句,在执行这个语句时假定该表达式为

true。如果表达式计算为

false,那么系统会报告一个

assertionerror。它用于调试目的:

assert(a

0);

//

throws

an

assertionerror

if

a

=

断言可以有两种形式:

assert

expression1

;

assert

expression1

:

expression2

;

expression1

应该总是产生一个布尔值。

expression2

可以是得出一个值的任意表达式。这个值用于生成显示更多调试信息的

string

消息。

断言在默认情况下是禁用的。要在编译时启用断言,需要使用

source

1.4

标记:

javac

-source

1.4

test.java

要在运行时启用断言,可使用

-enableassertions

或者

-ea

标记。

要在运行时选择禁用断言,可使用

-da

或者

-disableassertions

标记。

要系统类中启用断言,可使用

-esa

或者

-dsa

标记。还可以在包的基础上启用或者禁用断言。

可以在预计正常情况下不会到达的任何位置上放置断言。断言可以用于验证传递给私有方法的参数。不过,断言不应该用于验证传递给公有方法的参数,因为不管是否启用了断言,公有方法都必须检查其参数。不过,既可以在公有方法中,也可以在非公有方法中利用断言测试后置条件。另外,断言不应该以任何方式改变程序的状态。

第八,gc是什么?

为什么要有gc?

(基础)。

gc是垃圾收集器。java

程序员不用担心内存管理,因为垃圾收集器会自动进行管理。要请求垃圾收集,可以调用下面的方法之一:

system.gc()

runtime.getruntime().gc()

第九,string

s

=

new

string("xyz");创建了几个string

object?

两个对象,一个是“xyx”,一个是指向“xyx”的引用对象s。

第十,math.round(11.5)等於多少?

math.round(-11.5)等於多少?

math.round(11.5)返回(long)12,math.round(-11.5)返回(long)-11;

第二十一,abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?

都不能

在java中=和==的区别是什么?

=是赋值,==是判断。

1、a=b就是把b的值赋予给a。

2、a==b就是判断a等于b。

3、用==语句一般情况都是在要前面加if语句,作为判断ifa==b执行什么。

Java中RSA的方式如何实现非对称加密的示例

代码如下,需要依赖一个jar包commons-codec-1.9.jar,用于Base64转换,请自行下载。

import org.apache.commons.codec.binary.Base64;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import java.io.ByteArrayOutputStream;

import java.io.UnsupportedEncodingException;

import java.security.*;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

public class RSAUtils {

    // 加密方式

    public static final String ALGORITHM = "RSA";

    // 签名算法

    private static final String SIGNATURE_ALGORITHM = "SHA1WithRSA";

    // 创建密钥对初始长度

    private static final int KEY_SIZE = 512;

    // 字符编码格式

    private static final String CHARSET = "UTF-8";

    // RSA最大加密明文大小

    private static final int MAX_ENCRYPT_BLOCK = 117;

    // RSA最大解密密文大小

    private static final int MAX_DECRYPT_BLOCK = 128;

    private KeyFactory keyFactory;

    public RSAUtils() throws NoSuchAlgorithmException {

        keyFactory = KeyFactory.getInstance(ALGORITHM);

    }

    /**

     * 私钥加密

     *

     * @param content    待加密字符串

     * @param privateKey 私钥

     * @return 加密后字符串(BASE64编码)

     */

    public String encryptByPrivateKey(String content, String privateKey) throws Exception {

        String result;

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {

            byte[] keyBytes = new Base64().decode(privateKey);

            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

            PrivateKey pKey = keyFactory.generatePrivate(pkcs8KeySpec);

            Cipher cipher = Cipher.getInstance(ALGORITHM);

            cipher.init(Cipher.ENCRYPT_MODE, pKey);

            byte[] data = content.getBytes(CHARSET);

            write2Stream(cipher, data, out);

            byte[] resultBytes = out.toByteArray();

            result = Base64.encodeBase64String(resultBytes);

        } catch (Exception e) {

            throw new Exception(e);

        }

        return result;

    }

    /**

     * 公钥解密

     *

     * @param content   已加密字符串(BASE64加密)

     * @param publicKey 公钥

     * @return

     */

    public String decryptByPublicKey(String content, String publicKey) throws Exception {

        String result = "";

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {

            byte[] keyBytes = new Base64().decode(publicKey);

            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);

            PublicKey pKey = keyFactory.generatePublic(x509KeySpec);

            Cipher cipher = Cipher.getInstance(ALGORITHM);

            cipher.init(Cipher.DECRYPT_MODE, pKey);

            byte[] data = Base64.decodeBase64(content);

            write2Stream(cipher, data, out);

            byte[] resultBytes = out.toByteArray();

            result = new String(resultBytes);

        } catch (Exception e) {

            throw new Exception(e);

        }

        return result;

    }

    /**

     * 公钥加密

     *

     * @param content   待加密字符串

     * @param publicKey 公钥

     * @return 加密后字符串(BASE64编码)

     */

    public String encryptByPublicKey(String content, String publicKey) throws Exception {

        String result = "";

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {

            byte[] keyBytes = new Base64().decode(publicKey);

            X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);

            PublicKey pKey = keyFactory.generatePublic(x509KeySpec);

            Cipher cipher = Cipher.getInstance(ALGORITHM);

            cipher.init(Cipher.ENCRYPT_MODE, pKey);

            byte[] data = content.getBytes(CHARSET);

            write2Stream(cipher,

                    data, out);

            byte[] resultBytes = out.toByteArray();

            result = Base64.encodeBase64String(resultBytes);

        } catch (Exception e) {

            throw new Exception(e);

        }

        return result;

    }

    /**

     * 私钥解密

     *

     * @param content    已加密字符串

     * @param privateKey 私钥

     * @return 解密后字符串

     */

    public String decryptByPrivateKey(String content, String privateKey) throws Exception {

        String result = "";

        try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {

            byte[] keyBytes = new Base64().decode(privateKey);

            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

            PrivateKey pKey = keyFactory.generatePrivate(pkcs8KeySpec);

            Cipher cipher = Cipher.getInstance(ALGORITHM);

            cipher.init(Cipher.DECRYPT_MODE, pKey);

            byte[] data = Base64.decodeBase64(content);

            write2Stream(cipher, data, out);

            byte[] resultBytes = out.toByteArray();

            result = new String(resultBytes);

        } catch (Exception e) {

            throw new Exception(e);

        }

        return result;

    }

    private static void write2Stream(Cipher cipher, byte[] data, ByteArrayOutputStream out) throws

            BadPaddingException, IllegalBlockSizeException {

        int dataLen = data.length;

        int offSet = 0;

        byte[] cache;

        int i = 0;

        // 对数据分段解密

        while (dataLen - offSet  0) {

            if (dataLen - offSet  MAX_DECRYPT_BLOCK) {

                cache = cipher.doFinal(data, offSet, MAX_DECRYPT_BLOCK);

            } else {

                cache = cipher.doFinal(data, offSet, dataLen - offSet);

            }

            out.write(cache, 0, cache.length);

            i++;

            offSet = i * MAX_DECRYPT_BLOCK;

        }

    }

    /**

     * 用私钥对信息生成数字签名

     *

     * @param data       已加密数据

     * @param privateKey 私钥(BASE64编码)

     * @return sign

     */

    public String sign(String data, String privateKey) throws Exception {

        String result = "";

        try {

            byte[] keyBytes = new Base64().decode(privateKey);

            PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

            PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);

            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);

            signature.initSign(privateK);

            signature.update(parse2HexStr(data).getBytes(CHARSET));

            result = new Base64().encodeToString(signature.sign());

        } catch (Exception e) {

            throw new Exception(e);

        }

        return result;

    }

    /**

     * 校验数字签名

     *

     * @param data      已加密数据

     * @param publicKey 公钥(BASE64编码)

     * @param sign      数字签名

     * @return

     * @throws Exception

     */

    public boolean verify(String data, String publicKey, String sign) throws Exception {

        boolean result;

        try {

            byte[] keyBytes = new Base64().decode(publicKey);

            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

            PublicKey publicK = keyFactory.generatePublic(keySpec);

            Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);

            signature.initVerify(publicK);

            signature.update(parse2HexStr(data).getBytes(CHARSET));

            result = signature.verify(new Base64().decode(sign));

        } catch (Exception e) {

            throw new Exception(e);

        }

        return result;

    }

    /**

     * 将二进制转换成16进制

     *

     * @param data

     * @return

     */

    public static String parse2HexStr(String data) throws Exception {

        String result = "";

        try {

            byte[] buf = data.getBytes(CHARSET);

            StringBuffer sb = new StringBuffer();

            for (int i = 0; i  buf.length; i++) {

                String hex = Integer.toHexString(buf[i]  0xFF);

                if (hex.length() == 1) {

                    hex = '0' + hex;

                }

                sb.append(hex.toUpperCase());

            }

            result = sb.toString();

        } catch (UnsupportedEncodingException e) {

            throw new Exception(e);

        }

        return result;

    }

    /**

     * 生成公钥与私钥

     */

    public static void createKey() throws Exception {

        try {

            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);

            keyPairGenerator.initialize(KEY_SIZE);

            KeyPair keyPair = keyPairGenerator.generateKeyPair();

            RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();

            RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();

            String publicKey = Base64.encodeBase64String(rsaPublicKey.getEncoded());

            String privateKey = Base64.encodeBase64String(rsaPrivateKey.getEncoded());

            System.out.println("publicKey=" + publicKey + "\nprivateKey=" + privateKey);

        } catch (NoSuchAlgorithmException e) {

            throw new Exception(e);

        }

    }

    public static void main(String[] args) throws Exception {

        String PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKeYGXH6Vz+m+KuL11RDRaNdHB4YQeZgqpJGPRSwBelgvEoHu2/fNs1bmgfJhI8lhr/o/Hy8EFB/I/DDyLcCcU4bCLtxpki8edC+KJR2WvyYfnVmWEe/3q2jSVKRf81868q9Cd3MMfrQPMsY4x0TQ0GtOf/nMSMbAltV2W8J86IfAgMBAAECgYEApYu4lr2SMW3ddJZNvQ42W4g9nfyYG9igpJx8+VJmhIDpfLbmjzsOBwvUupx0NHH9CNQ7k3qxItJzzf+W5C+lesEOAqdO5nahRZsL8BIDoxTEn2j+1GXkzQw3vqPY50xqRnZsoP5TyNNsOM7KYaOoz4VFMdVIFwoT3OKM5z7mxgECQQD51r17WZDSa/kucaH7gCOePxJPS6Ust0eVd5tBOMpFzD/VtziogSIWyhGKkLH0SyTJEe91CCpdpxufLSZgIiN5AkEAq7ojtvU4twak1N1/1qX+t8f5wD8i/8GU702PeCwkGI5ymrARq+W2yCuvU1bouXBhjKHV4KhafKYixdHUMg00VwJAYVUjpLaUESY3gbyLWqvlNHVl8LaLtwwAO17JgXNaei7Ef8JNtHf6i95VTyJn8cCEqEDwhSuVNb8wp6azWKh0IQJBAJHrcT2d0bt0IcvfCynRk0eG3WnGPG8mhu9w8GAk4ecb47YdtmZio5YjyK8AQnCQVdOyEJL9eyY/5XxCeBSvs7ECQQCKQ2f5HLDkkHvc6rlaHCJmqNRCS+CxhoaiaSPYLAac7WXmb614ACLECc86C/nkefTq0SNpUDVbGxVpJi9/FOUf";

        String PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnmBlx+lc/pviri9dUQ0WjXRweGEHmYKqSRj0UsAXpYLxKB7tv3zbNW5oHyYSPJYa/6Px8vBBQfyPww8i3AnFOGwi7caZIvHnQviiUdlr8mH51ZlhHv96to0lSkX/NfOvKvQndzDH60DzLGOMdE0NBrTn/5zEjGwJbVdlvCfOiHwIDAQAB";

        RSAUtils rsaUtil = new RSAUtils();

        String encryptByPublicKey = rsaUtil.encryptByPublicKey("你好!", PUBLIC_KEY);

        System.out.println(encryptByPublicKey);

        String decryptByPrivateKey = rsaUtil.decryptByPrivateKey(encryptByPublicKey, PRIVATE_KEY);

        System.out.println(decryptByPrivateKey);

        String encryptByPrivateKey = rsaUtil.encryptByPrivateKey("你好!", PRIVATE_KEY);

        System.out.println(encryptByPrivateKey);

        String decryptByPublicKey = rsaUtil.decryptByPublicKey(encryptByPrivateKey, PUBLIC_KEY);

        System.out.println(decryptByPublicKey);

        String sign = rsaUtil.sign("1234", PRIVATE_KEY);

        System.out.println("sign:" + sign);

        System.out.println(rsaUtil.verify("1234", PUBLIC_KEY, sign));

    }

}

在java中=和==的区别

"="是赋值操作符,它的操作是把符号右边的变量或者常量或者对象的值赋值给符号右边的变量。

"=="是算数运算符中的一个操作符,主要用在判断中,如if(a

==

b){...}这条语句中,a

==

b语句的意思是先比较a和b的值是否相等(java中的相等不止是有值相等,还有对象的引用相等,这在比较两个值相等时需要注意和分清),然后返回比较的结果(只有两张,true和false)。

javaifajlx的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于、javaifajlx的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-24,除非注明,否则均为首码项目网原创文章,转载请注明出处。