5 Replies - 340 Views - Last Post: 14 April 2012 - 02:27 PM Rate Topic: -----

#1 junny013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 12

wrong in this code

Posted 13 April 2012 - 02:36 PM

public class Read
{
	public static void main (String []args)
	{
		int a = 50;
		if (a !=null)
		{
		System.out.println("a =" +a);}
		else
		System.out.print("no");
	}
}



i got a message "Read.java:9: incomparable types: int and <nulltype>"

I want to check the null type
What should i do to fix this thing?
WHat should I use if I am not declare "int a = 50"
I want to keep the part from
              if (a !=null)
		{
		System.out.println("a =" +a);}
		else
		System.out.print("no");



So i want to fix the part int a = 50
How can I fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: wrong in this code

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: wrong in this code

Posted 13 April 2012 - 02:41 PM

The compiler is telling you that the comparison of an int variable type to null does not make sense. You set a = 50, and then compare it to null. Why do you want to do that? What are you trying to do?
Was This Post Helpful? 0
  • +
  • -

#3 junny013  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 08-April 12

Re: wrong in this code

Posted 13 April 2012 - 04:01 PM

View PostGregBrannon, on 13 April 2012 - 02:41 PM, said:

The compiler is telling you that the comparison of an int variable type to null does not make sense. You set a = 50, and then compare it to null. Why do you want to do that? What are you trying to do?

I am trying to fix the int a = 50 part
I actually dont understand much about "null", so i want to check it.
DO you know what should I put instead of "int a = 50;" ?
keep the rest .
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: wrong in this code

Posted 13 April 2012 - 04:17 PM

If you were to answer MY questions, I might be able to answer yours. I can't help you do something, if I don't know what you're trying to do.

You can learn more about null here.
Was This Post Helpful? 0
  • +
  • -

#5 Magooguli  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 34
  • Joined: 09-April 12

Re: wrong in this code

Posted 13 April 2012 - 06:13 PM

If you're trying to check whether a declared variable has been given a value (which would be redundant because od your assigning it a value) you are unable to as integers declared in Java are given a default value of '0'.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: wrong in this code

Posted 14 April 2012 - 02:27 PM

View PostMagooguli, on 13 April 2012 - 09:13 PM, said:

If you're trying to check whether a declared variable has been given a value (which would be redundant because od your assigning it a value) you are unable to as integers declared in Java are given a default value of '0'.

If they are instance variable which is not the case here :)

Back to OP question:
A basic dadatype variable is not an object, it can't be compare to null to know if initialized or not
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1