Java and or syntax error

Top 8 Common Java Errors for Beginners — and How to Solve Them

Did you know that in Java’s standard library, there are a total of more than 500 different exceptions! There lots of ways for programmers to make mistakes — each of them unique and complex. Luckily we’ve taken the time to unwrap the meaning behind many of these errors, so you can spend less time debugging and more time coding. To begin with, let’s have a look at the syntax errors!

Syntax Errors

If you just started programming in Java, then syntax errors are the first problems you’ll meet! You can think of syntax as grammer in English. No joke, syntax errors might look minimal or simple to bust, but you need a lot of practice and consistency to learn to write error-free code. It doesn’t require a lot of math to fix these, syntax just defines the language rules. For further pertinent information, you may refer to java syntax articles.

Читайте также:  Java class for constants

Working with semicolons (;)

Think of semi-colons (;) in Java as you think of a full-stop (.) in English. A full stop tells readers the message a sentence is trying to convey is over. A semi-colon in code indicates the instruction for that line is over. Forgetting to add semi-colons (;) at the end of code is a common mistake beginners make. Let’s look at a basic example.

This snippet will produce the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error, insert ";" to complete BlockStatements at topJavaErrors.JavaErrors.main(JavaErrors.java:3)

You can resolve this error by adding a ; at the end of line 3.

Braces or parentheses [(), <>]

Initially, it can be hard keeping a track of the starting / closing parenthesis or braces. Luckily IDEs are here to help. IDE stands for integrated development environment, and it’s a place you can write and run code. Replit for example, is an IDE. Most IDEs have IntelliSense, which is auto-complete for programmers, and will add closing brackets and parentheses for you. Despite the assistance, mistakes happen. Here’s a quick example of how you can put an extra closing bracket or miss the ending brace to mess up your code.

If you try to execute this, you’ll get the following error.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token ")", delete this token at topJavaErrors.SyntaxErrors.main(SyntaxErrors.java:11)

You can resolve this exception by removing the extra ) on line 9.

My full name is: Justin Delaware 

Double Quotes or Quotation Marks (“ ”)

Another pit fall is forgetting quotation marks not escaping them propperly. The IntelliSense can rescue you if you forget the remaining double quotes. If you try including quotation marks inside strings, Java will get confused. Strings are indicated using quotation marks, so having quotation marks in a string will make Java think the string ended early. To fix this, add a backslash (\) before quotation marks in strings. The backslash tells Java that this string should not be included in the syntax.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Syntax error on token "Java", instanceof expected The preview feature Instanceof Pattern is only available with source level 13 and above is cannot be resolved to a type Syntax error, insert ")" to complete MethodInvocation Syntax error, insert ";" to complete Statement The method favourtie(String) is undefined for the type SyntaxErrors Syntax error on token "language", ( expected at topJavaErrors.SyntaxErrors.main(SyntaxErrors.java:5)

In order for you avoid such exceptions, you can add backslahes to the quotes in the string on line 4.

What did Justin say? Justin said, "Java is my favourite language"

Here’s your required output, nicely put with double quotes! 🙂

Other Miscellaneous Errors

Accessing the “Un-Initialized” Variables

If you’re learning Java and have experience with other programming languages (like C++) then you might have a habit of using un-initialized variables (esp integers). Un-initialized variables are declared variables without a value. Java regulates this and doesn’t allow using a variable that has not been initialized yet.

If you attempt to access an uninitialized variable then you’ll get the following exception.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The local variable contactNumber may not have been initialized at topJavaErrors.UninitialziedVars.main(UninitialziedVars.java:5)

You can initialize the variable “contactNumber” to resolve this exception.

int contactNumber = 9935856;

Accessing the “Out of Scope” Variables

If you define a variable in a certain method you’re only allowed to access that in the defined scope of it. Like each state has their legitimate currency, and that cannot be used in another state. You cannot use GBP in place of USD in America. Similarly, a variable defined in one method has restricted scope to it. You cannot access a local variable defined in some function in the main method. For further detailed illustration let’s look at an example.

As soon as you run this snippet, you’ll get the exception

Exception in thread "main" java.lang.Error: Unresolved compilation problem: country cannot be resolved to a variable at topJavaErrors.OutOfScopeVars.main(OutOfScopeVars.java:9)

You can not access the variable “country” outside the method getPersonalDetails since its scope is local.

Modifying the “CONSTANT” Values

Java and other programming languages don’t allow you to update or modify constant variables. You can use the keyword “final” before a variable to make it constant in Java. Apart from that, it’s a convention to write a constant in ALL CAPS for distinction purposes, As a constant resource is often used cross methods across a program.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: The final field ConstVals.SSN cannot be assigned at topJavaErrors.ConstVals.main(ConstVals.java:5)

Remove line 4 for the perfectly functional code.

Misinterpretted Use of Operators ( == vs .equals())

A lot of beginners start working with integers while learning the basics of a programming language. So it can be a challenge to remember that for string comparisons we use the “.equals()” method provided by Java and not == operator like in integers.

It's not a Wednesday! It's a Wednesday!

The output is contradictory because “today” and “thirdWeekDay” are referred to 2 different objects in the memory. However, the method “.equals()” compares the content stored in both arrays and returns true if it’s equal, false otherwise.

Accessing a non-static resource from a static method

If you want to access a non-static variable from a static method [let’s say the main method] then you need to create an instance of that object first. But if you fail to do that, java will get angry.

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field postalCode at topJavaErrors.NonStaticAccess.main(NonStaticAccess.java:17)

You can fix it, just by replacing line 9.

// Accessing the non-static member variable // by creating an instance of the object System.out.println("What's the postal code of your area? " + address.postalCode);

Conclusion

Programming errors are a part of the learning curve. Frequent errors might slow you down. But as a new programmer, it’s okay to learn things slowly. Now you’re familiar with some of the most common issues. Make sure you practise enough to get ahead of them. Happy coding and keep practising! 🙂

Источник

Java Operator – &, && (AND) || (OR) Logical Operators

Ihechikara Vincent Abba

Ihechikara Vincent Abba

Java Operator – &, && (AND) || (OR) Logical Operators

We use operators in most programming languages to perform operations on variables.

They are divided into various categories like arithmetic operators, assignment operators, comparison operators, logical operators, and so on.

In this article, we will be talking about the bitwise AND operator, and the AND ( && ) and OR ( || ) logical operators.

How to use the bitwise AND operator

The symbol & denotes the bitwise AND operator. It evaluates the binary value of given numbers. The binary result of these numbers will be returned to us in base 10.

When the & operator starts its operation, it will evaluate the value of characters in both numbers starting from the left.

Let’s look at an example to help you understand better:

System.out.println(10 & 12); // returns 8

The binary value of 10 is 1010

The binary value of 12 is 1100

Here is something you should have in mind before we start the operation:

So let’s carry out the operation.

The first character for 10 is 1 and the first character for 12 is also 1 so:

We move on to the second characters – 0 for 10 and 1 for 12:

For the third characters – 1 for 10 and 0 for 12:

For the fourth characters – 0 for 10 and 0 for 12:

Now let’s combine all the returned characters. We would have 1000.

The binary value 1000 in base 10 is 8 and that is why our operation returned 8.

How to use the logical AND operator

Note that we use logical operators to evaluate conditions. They return either true or false based on the conditions given.

The symbol && denotes the AND operator. It evaluates two statements/conditions and returns true only when both statements/conditions are true.

Here is what the syntax looks like:

statment1/condition1 && statemnt2/condition2

As you can see above, there are two statements/conditions separated by the operator. The operator evaluates the value of both statements/conditions and gives us a result – true or false.

System.out.println((10 > 2) && (8 > 4)); //true

The operation will return true because both conditions are true – 10 is greater than 2 and 8 is greater than 4. If either one of the conditions had an untrue logic then we would get false .

To better understand the && operator, you should know that both conditions must be true to get a value of true .

Here is another example that returns false :

System.out.println((2 > 10) && (8 > 4)); // false

Here, 2 is not greater than 10 but 8 is greater than 4 – so we get a false returned to us. This is because one of the conditions is not true.

  • If both conditions are true => true
  • If one of the two conditions is false => false
  • If both conditions are false => false

How to use the logical OR operator

We use the symbol || to denote the OR operator. This operator will only return false when both conditions are false. This means that if both conditions are true, we would get true returned, and if one of both conditions is true, we would also get a value of true returned to us.

statment1/condition1 || statemnt2/condition2

Let’s go over a few examples.

System.out.println((6 < 1) || (4 >2)); // true

This returns true because one of conditions is true.

  • If both conditions are true => true
  • If one of the conditions is true => true
  • If both conditions are false => false

Conclusion

In this article, we learned how to use the bitwise & operator in Java and how the operation is carried out to give us a result.

We also learned how to use the && and || logical operators in Java. We learned what value each operation returns based on the conditions involved in the operation.

Источник

Оцените статью