Home  >  Article  >  Java  >  Details that need to be paid attention to in Java programming

Details that need to be paid attention to in Java programming

无忌哥哥
无忌哥哥Original
2018-07-18 11:40:101483browse

As the saying goes: "Details determine success or failure", and this is especially true for programming.

Recently, I participated in a project that made me realize this deeply.

This project is part of the recommendation system. The part I am responsible for is MapRedcue programming to calculate the similarity between videos! Another colleague used Sqoop to import the results I calculated into the database for online calling.

During the process of importing data, a java.lang.NumberFormatException always occurs. I think it is because his Sqoop does not filter blank lines or spaces. My similarity result is no Incorrect. Later, he copied part of the similarity results into Notepad and found that some data had spaces. He read it in directly and then converted it, but of course something went wrong. I don't know if Sqoop can filter spaces, but my program can control the output results, so I call the trim() method of the java.lang.String class for each output result. Eventually the problem is solved.

Spaces will always cause some minor problems, but they cannot be ignored. To eliminate spaces, remember to call the trim() method.

Another problem is that when comparing encapsulation classes of basic data types, it may not be the numerical values ​​being compared, but the memory addresses!

In the process of similarity calculation, there is a place where equality judgment between java.lang.Long types is required. When I found that the final similarity output result was incorrect, I investigated the cause and finally landed here. The original code is like this:

if(lg1 == lg2){
    return true;
}

lg1 and lg2 are both of java.lang.Long type. Here I want to determine whether the values ​​​​of lg1 and lg2 are equal. Before, I just generally remembered that Java The encapsulated class will be converted when compared, so I wrote it like this! But writing it this way may also be larger, not the numerical value, but their respective addresses in memory.

I searched this question on Google. It was said on the Internet that the java.lang.Float type and the java.lang.Double type use "==" to make equality judgments and need to be converted into basic data types, java.lang.Integer. and java.lang.Long do not need to be converted, Java will automatically convert them. However, my own tests contradict this statement. The following is the test code:

package org.jindao.basic;

/**
 * @author 
 * @date 2013年10月25日 上午7:30:47
 */
public class BasicTest {
	public static void main(String[] args) {
		Integer ig1 = 3;
		Integer ig2 = 3;
		System.out.println("Integer ig1 = 3,Integer ig2 = 3  ig1==ig2的结果为:"+(ig1==ig2));
		
		Integer ig3 = new Integer(3);
		Integer ig4 = new Integer(3);
		System.out.println("Integer ig3 = new Integer(3),Long ig4 = new Integer(3) ig3==ig4的结果为:"+(ig3==ig4));
		
		Long lg1 = 3l;
		Long lg2 = 3l;
		System.out.println("Long lg1 = 3l,Long lg2 = 3l lg1==lg2的结果为:"+(lg1==lg2));
		
		Long lg3 = new Long(3);
		Long lg4 = new Long(3);
		System.out.println("Long lg3 = new Long(3),Long lg4 = new Long(3) lg3==lg4的结果为:"+(lg3==lg4));
		
		Float flt1 = 3.2f;
		Float flt2 = 3.2f;
		System.out.println("Float flt1 = 3.2f,Float flt2 = 3.2f  flt1==flt2的结果为:"+(flt1==flt2));
		
		Float flt3 = new Float(3.2);
		Float flt4 = new Float(3.2);
		System.out.println("Float flt3 = new Float(3.2),Float flt4 = new Float(3.2)) flt3==flt4的结果为:"+(flt3==flt4));
		
		Double db1 = 3.2;
		Double db2 = 3.2;
		System.out.println("Double db1 = 3.2,Double db2 = 3.2 db1==db2的结果为:"+(db1==db2));
		
		Double db3 = new Double(3.2);
		Double db4 = new Double(3.2);
		System.out.println("Double db3 = new Double(3.2),Double db4 = new Double(3.2) db3==db4的结果为:"+(db3==db4));
	}
}

Running results:

Integer ig1 = 3,Integer ig2 = 3  ig1==ig2的结果为:true
Integer ig3 = new Integer(3),Long ig4 = new Integer(3) ig3==ig4的结果为:false
Long lg1 = 3l,Long lg2 = 3l lg1==lg2的结果为:true
Long lg3 = new Long(3),Long lg4 = new Long(3) lg3==lg4的结果为:false
Float flt1 = 3.2f,Float flt2 = 3.2f  flt1==flt2的结果为:false
Float flt3 = new Float(3.2),Float flt4 = new Float(3.2)) flt3==flt4的结果为:false
Double db1 = 3.2,Double db2 = 3.2 db1==db2的结果为:false
Double db3 = new Double(3.2),Double db4 = new Double(3.2) db3==db4的结果为:false

As can be seen from the results, only when Integer and Long types are directly assigned values, "==" can be used to judge equality. will be true, and will be false the rest of the time.

That is to say, the rest of the cases are mostly the address where the variable is stored in the memory, not the value of the variable.

Then why do Integer and Long types directly assign values ​​​​and the result is true, while Float and Double types directly assign values ​​​​and the result is false? I guess that Java itself has optimization measures, that is, when directly assigning values ​​to the Integer and Long types, objects of the Integer and Long types are not created in the memory, but are directly optimized to the basic data types int and long, so "= is used. ="The result will be true only if they are equal.

To be on the safe side, when it is judged that the values ​​of encapsulated class variables are equal, it is best to directly take out the values ​​​​for "==" judgment, or use the equals method, that is,

lg1.equals(lg2)

Small details like these , if you don’t figure it out, it may cause trouble at some point.

Details determine success or failure. There are many details that need to be paid attention to in Java programming. Only one or two will be discussed here.

The above is the detailed content of Details that need to be paid attention to in Java programming. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn