Cookie Consent by Free Privacy Policy Generator 📌 Common Errors and Pitfalls


✅ Common Errors and Pitfalls


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

Common elementary programming errors often involve undeclared variables, uninitialized variables, integer overflow, unintended integer division, and round-off errors

Common Error 1: Undeclared/Uninitialized Variables and Unused Variables

A variable must be declared with a type and assigned a value before using it. A common error is not declaring a variable or initializing a variable. Consider the following code:

double interestRate = 0.05;
double interest = interestrate * 45;

This code is wrong, because interestRate is assigned a value 0.05; but interestrate has not been declared and initialized. Java is case sensitive, so it considers interestRate and interestrate to be two different variables.

If a variable is declared, but not used in the program, it might be a potential programming error. So, you should remove the unused variable from your program. For example, in the following code, taxRate is never used. It should be removed from the code.

double interestRate = 0.05;
double taxRate = 0.05;
double interest = interestRate * 45;
System.out.println("Interest is " + interest);

If you use an IDE such as Eclipse and NetBeans, you will receive a warning on unused
variables.

Common Error 2: Integer Overflow

Numbers are stored with a limited numbers of digits. When a variable is assigned a value that is too large (in size) to be stored, it causes overflow. For example, executing the following statement causes overflow, because the largest value that can be stored in a variable of the int type is 2147483647. 2147483648 will be too large for an int value.

int value = 2147483647 + 1;
// value will actually be -2147483648

Likewise, executing the following statement causes overflow, because the smallest value that can be stored in a variable of the int type is -2147483648. -2147483649 is too large in size to be stored in an int variable.

int value = -2147483648 - 1;
// value will actually be 2147483647

Java does not report warnings or errors on overflow, so be careful when working with numbers close to the maximum or minimum range of a given type.
When a floating-point number is too small (i.e., too close to zero) to be stored, it causes underflow. Java approximates it to zero, so normally you don’t need to be concerned about underflow.

Common Error 3: Round-off Errors

A round-off error, also called a rounding error, is the difference between the calculated approximation of a number and its exact mathematical value. For example, 1/3 is approximately 0.333 if you keep three decimal places, and is 0.3333333 if you keep seven decimal places. Since the number of digits that can be stored in a variable is limited, round-off errors are inevitable. Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy. For example,

System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);

displays 0.5000000000000001, not 0.5, and

System.out.println(1.0 - 0.9);

displays 0.09999999999999998, not 0.1. Integers are stored precisely. Therefore, calculations with integers yield a precise integer result.

Common Error 4: Unintended Integer Division

Java uses the same divide operator, namely /, to perform both integer and floating-point division. When two operands are integers, the / operator performs an integer division. The result of the operation is an integer. The fractional part is truncated. To force two integers to perform a floating-point division, make one of the integers into a floating-point number. For example, the code in (a) displays that average is 1 and the code in (b) displays that average is 1.5.

int number1 = 1;
int number2 = 2;
double average = (number1 + number2) / 2;
System.out.println(average);

(a)

int number1 = 1;
int number2 = 2;
double average = (number1 + number2) / 2.0;
System.out.println(average);

(b)

Common Pitfall 1: Redundant Input Objects

New programmers often write the code to create multiple input objects for each input. For example, the following code reads an integer and a double value.

Here are bad code:

Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int v1 = input.nextInt();
Scanner input1 = new Scanner(System.in);
System.out.print("Enter a double value: ");
double v2 = input1.nextDouble();

The code is not wrong, but inefficient. It creates two input objects unnecessarily and may lead to some subtle errors. You should rewrite the code as follows:

Here's good code:

Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int v1 = input.nextInt();
System.out.print("Enter a double value: ");
double v2 = input.nextDouble();
...

✅ Common Errors and Pitfalls


📈 39.8 Punkte

✅ Common Errors and Pitfalls in selection statements


📈 39.8 Punkte

✅ Next.js in Production: Best Practices and Common Pitfalls


📈 27.11 Punkte

✅ 5 common data security pitfalls — and how to avoid them


📈 27.11 Punkte

✅ Why You're Not Improving as a Developer: Common Pitfalls and How to Overcome Them


📈 27.11 Punkte

✅ Mastering React Hooks: Best Practices and Common Pitfalls


📈 27.11 Punkte

✅ Building a Secure dApp: Best Practices and Common Pitfalls


📈 27.11 Punkte

✅ Common Pitfalls in Machine Learning Model Inference for Beginners and How to Solve Them


📈 27.11 Punkte

✅ OWASP BeNeLux Day Common REST API security pitfalls by Philippe De Ryck


📈 25.57 Punkte

✅ Categorization of Common Information Security Pitfalls


📈 25.57 Punkte

✅ Common API Security Pitfalls


📈 25.57 Punkte

✅ Wekan Authentication Bypass – Exploiting Common Pitfalls of MeteorJS


📈 25.57 Punkte

✅ How to Avoid Common Pitfalls Associated with IGA Implementations


📈 25.57 Punkte

✅ Digital Transformation Success: Common Pitfalls to Avoid


📈 25.57 Punkte

✅ 7 Common Security Pitfalls to Avoid When Migrating to the Cloud


📈 25.57 Punkte

✅ 7 Common Security Pitfalls to Avoid When Migrating to the Cloud


📈 25.57 Punkte

✅ Navigating a Value Stream Management Journey? Here are 4 Common Pitfalls to Avoid


📈 25.57 Punkte

✅ Avoiding Common Pitfalls when Working with React JS 🚫


📈 25.57 Punkte

✅ 6 Common Pitfalls of Information Security Risk Assessments


📈 25.57 Punkte

✅ Common pitfalls to avoid when optimizing code performance


📈 25.57 Punkte

✅ Avoiding common pitfalls with ControlValueAccessors in Angular


📈 25.57 Punkte

✅ A Decade of Lessons Learned: Common Pitfalls in Software Engineering


📈 25.57 Punkte

✅ Avoid Common Pitfalls with Next.js Server-Side Rendering


📈 25.57 Punkte

✅ Mastering React Development: Avoiding Common Pitfalls.


📈 25.57 Punkte

✅ Database Migration Plan: Avoiding Common Pitfalls in Open Source Migration


📈 25.57 Punkte

✅ Choosing the Right AWS EC2 Instance: Avoiding Common Pitfalls


📈 25.57 Punkte

✅ Mastering useState: A Guide to Avoiding Common Pitfalls in React Development


📈 25.57 Punkte

✅ Avoiding Pitfalls: Common Challenges in Backend Development with Spring Boot


📈 25.57 Punkte

✅ Common Pitfalls of Running On-Premises SIEM Solutions


📈 25.57 Punkte

✅ Avoiding Common Pitfalls When First Contributing To Open Source


📈 25.57 Punkte

✅ 10 Common Pitfalls to Avoid When Purchasing Online Courses


📈 25.57 Punkte

✅ React Performance Pitfalls: Avoiding Common Mistakes


📈 25.57 Punkte

✅ Common Pitfalls When Using React Hooks


📈 25.57 Punkte

✅ 7 common pitfalls in JSON operations in Golang


📈 25.57 Punkte











matomo

Datei nicht gefunden!