当我的编译器告诉我我正在使用不安全或未经检查的操作时,这是什么意思? [英] What does it mean when my compiler tells me I'm using unsafe or unchecked operations?

查看:64
本文介绍了当我的编译器告诉我我正在使用不安全或未经检查的操作时,这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序可以正常编译,但是我的控制台吐出了以下内容:

My program compiles fine, but my console spits out the following:

 ----jGRASP exec: javac -g CreditGraphics.java

Note: CreditGraphics.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

 ----jGRASP: operation complete.

首先,是什么使操作不安全?以及如何取消检查"流程?使用-Xlint重新编译"是什么意思?我正在使用jGrasp,但不确定是按钮还是某种命令?我想看看细节.它没有指定不安全或未经检查的内容,但是无论如何这是我的代码:

First, what makes an operation unsafe? And how can a process be "unchecked"? What does "Recompile with -Xlint" mean? I'm using jGrasp and I'm not sure if that's a button or some sort of command? I want to see the details. It doesn't specify what is unsafe or unchecked, but here's my code anyway:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.util.Scanner;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.event.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Properties;
import javax.mail.Header;
import java.util.Enumeration;
import java.util.Properties;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JApplet;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.URLName;
import java.beans.*;
import java.util.ArrayList;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import java.util.Scanner;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
import javax.swing.Timer;

public class CreditGraphics {

    //first screen variables
    public String cardNum;
    public JFrame frame;
    public JPanel panel;
    public JLabel label;
    public JTextField text;
    public String cardType = "";
    public String carddigits;
    public boolean cardValid;
    public int length;
    public String[] cardTypes;
    public JComboBox cardTypesDD;
    public static ArrayList<Integer> holdDigits = new ArrayList<Integer>();
    public static ArrayList<String> holdDigitsChar = new ArrayList<String>();
    public static int[] checkDigits;
    public static int checkSum = 0;

    //second screen variables in verifyScreen;
    public JFrame vframe;
    public JPanel vpanel;
    public JLabel titlelabel;
    public GridBagConstraints grid;
    public JLabel namelabel;
    public JTextField namefield;

    public CreditGraphics() {

        frame = new JFrame("MES Banking App");
        panel = new JPanel();
        label = new JLabel();
        cardTypes = new String[4];
        cardTypes[0] = "Visa";
        cardTypes[1] = "American Express";
        cardTypes[2] = "Master Card";
        cardTypes[3] = "";
        cardTypesDD = new JComboBox(cardTypes);
        cardTypesDD.setSelectedIndex(3);

        text = new JTextField(16);

        panel.add(label);
        panel.add(cardTypesDD);
        panel.add(text);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        panel.setPreferredSize(new Dimension(500, 500));
        frame.getContentPane().add(panel);

        frame.pack();
        frame.setVisible(true);

        label.setText("<html>Please enter your credit card <br> 'Master Card' 'Visa' or 'American Express'</html>");

        text.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //after CC type is entered, prompt user to enter digits
                        carddigits = text.getText();//gets credit card number from jtextfield
                        length = carddigits.length();//sets length of card
                        validateCard(); //uses credit card number and length to determine if it matches up to brand

                        //below returns if card is valid
                        if (cardValid == true) {
                            label.setText("Card brand is valid");
                        }
                        waits(1);

                        text.setText("");

                        waits(1);
                        checkDigits();

                    }

                });

        cardTypesDD.addActionListener( new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        //where program really starts
                        while (cardTypesDD.getSelectedIndex() == 3) {
                            label.setText("First, please select a card type from DD list");
                        }

                        cardType = (String) cardTypesDD.getSelectedItem();
                        System.out.println(cardType);

                        if (!cardType.equals("")) {
                            label.setText("Thank you, now please enter your card #");
                        }
                        //now go to the jtextfield actionlistener
                    }});
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {

                        new CreditGraphics();

                    }});
    }

    public void verifyScreen() {
        //destroy old frame
        frame.dispose();

        //create new frame essentially same as last frame
        vframe = new JFrame("MES Banking App");
        vpanel = new JPanel();
        titlelabel = new JLabel("Verification Page");
        namelabel = new JLabel("Name: ");
        namefield = new JTextField(20);
        //title section
        vpanel.setLayout(new GridBagLayout());
        grid = new GridBagConstraints();
        grid.fill = GridBagConstraints.PAGE_START;
        grid.weightx = 0;
        grid.gridx = 0;
        grid.gridy = 0;
        vpanel.add(titlelabel, grid);

        //name section
        grid.gridy = 1;
        grid.insets = new Insets(10, 0, 0, 0);
        vpanel.add(namelabel, grid);
        grid.gridx = 1;
        grid.insets = new Insets(10, 10, 0, 0);
        vpanel.add(namefield, grid);

        vframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        vframe.getContentPane().add(vpanel);

        vframe.pack();
        vframe.setVisible(true);
    }

    public static void waits(int k) {
        long time0, time1;
        time0 = System.currentTimeMillis();
        do {
            time1 = System.currentTimeMillis();
        } while ((time1 - time0) < k * 1000);
    }

    public void validateCard() {
        //check brand

        cardValid = false;
        if ((cardType.equals("Visa") && carddigits.substring(0, 1).equals("4")) && (length == 13 || length == 16)) {
            label.setText("Thank you, next step");

            cardValid = true;
        }

        if ((cardType.equals("Master Card")) && (carddigits.substring(0, 2).equals("51") || carddigits.substring(0, 2).equals("52") || carddigits.substring(0, 2).equals("53") || carddigits.substring(0, 2).equals("54") || carddigits.substring(0, 2).equals("55")) && (length == 16)) {
            label.setText("Thank you, next step");
            cardValid = true;
        }

        if ((cardType.equals("American Express") && carddigits.substring(0, 2).equals("37") && length == 15)) {
            label.setText("Thank you, next step");
            cardValid = true;
        }

        if (cardValid != true) {
            System.out.println("ERROR");
            label.setText("ERROR");
            waits(2);
            System.exit(0);
        }
        //end check
    }

    public void checkDigits() {
        label.setText("Checking digits using Luhn's algorithm...");
        waits(2);
        //check digits
        checkDigits = new int[length];
        for (int i = 0; i < length; i++) {
            checkDigits[i] = Integer.parseInt(carddigits.substring(i, i + 1));

            //successfully puts digits into array
        }
        for (int e = length - 2; e >= 0; e -= 2) {
            checkDigits[e] = 2 * checkDigits[e];
        }
        for (int d = 0; d < length; d++) {
            holdDigitsChar.add(String.valueOf(checkDigits[d]));
        }
        for (int v = 0; v < length; v++) {
            if (holdDigitsChar.get(v).length() == 2) {
                holdDigits.add(Integer.parseInt(holdDigitsChar.get(v).substring(0, 1)));
                holdDigits.add(Integer.parseInt(holdDigitsChar.get(v).substring(1, 2)));
            } else {
                holdDigits.add(Integer.parseInt(holdDigitsChar.get(v)));
            }
        }

        for (int c = 0; c < holdDigits.size(); c++) {
            checkSum += holdDigits.get(c);
        }

        System.out.println("Check sum:" + checkSum);

        if (checkSum % 10 == 0) {
            label.setText("Numbers check out, thank you");
            waits(2);
            verifyScreen();
        } else {
            System.out.println("ERROR");
            System.exit(0);
        }
    }
}

推荐答案

如果您使用的是集合或其他没有通用参数的通用对象,通常会出现这种情况.例如:

This usually comes up if you're using collections or some other genericized object without generic parameters. For example:

List l = new ArrayList();

vs.

List<String> l = new ArrayList<>(); //or new ArrayList<String>(); in Java < 7

这意味着Java编译器不能保证您以类型安全的方式使用这些集合.例如,在第一种情况下,应将 String Integer 放入 ArrayList .在某个时候将其拔出时,很可能会尝试将 Integer 实例转换为 String 的实例,这将导致 ClassCastException.当然,您可以确实真正小心,不要这样做,但是编译器只是在提醒您以下事实:没有办法保证什么是真实的.在该列表中.

What this means is that the Java compiler cannot guarantee that you are using those collections in a type-safe way. For example, you should shove a String and an Integer into the ArrayList in the first scenario. At some point when you pull it out, there is a distinct possibility that you may attempt to cast the Integer instance into a String, which would result in a ClassCastException. You could, of course, be really, really, careful and not do this, but the compiler is simply alerting you to the fact that there is no way to guarantee what is inside that list.

要摆脱此警告,请使用第二种实例化方法.如果您确定可以解决这个问题(在某些情况下有可能,因为您可以确定集合中将包含哪些内容),则可以使用 @SuppressWarnings("unchecked")注释.

To get rid of this warning, use the second method of instantiation. If you are sure that you can get away with this (in some instances it is possible because you can be sure what the collection will contain) you can use the @SuppressWarnings("unchecked") annotation.

这篇关于当我的编译器告诉我我正在使用不安全或未经检查的操作时,这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆