Home Java javaTutorial Details that need to be paid attention to in Java programming

Details that need to be paid attention to in Java programming

Jul 18, 2018 am 11:40 AM

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;
}
Copy after login

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));
	}
}
Copy after login

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
Copy after login

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)
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? What is the difference between memory leaks in Java programs on ARM and x86 architecture CPUs? Apr 19, 2025 pm 11:18 PM

Analysis of memory leak phenomenon of Java programs on different architecture CPUs. This article will discuss a case where a Java program exhibits different memory behaviors on ARM and x86 architecture CPUs...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to convert names to numbers to implement sorting within groups? How to convert names to numbers to implement sorting within groups? Apr 19, 2025 pm 01:57 PM

How to convert names to numbers to implement sorting within groups? When sorting users in groups, it is often necessary to convert the user's name into numbers so that it can be different...

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? How to correctly divide business logic and non-business logic in hierarchical architecture in back-end development? Apr 19, 2025 pm 07:15 PM

Discussing the hierarchical architecture problem in back-end development. In back-end development, common hierarchical architectures include controller, service and dao...

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? Apr 19, 2025 pm 01:51 PM

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

See all articles