1

I am a beginner and I have searched and search and cannot find the answer to this question. I understand that int test = (int) (5.4); is casting a double to an int but what I do not understand is what (int) is. It does not seem to be written as a method or a class. It is written in a unique way that I have not seen yet and I am attempting to understand why this statement works in Java. I would have expected something like int test = Math.int(5.4). I do not understand why this statement works. I hope this make sense.

Dimitar
  • 4,402
  • 4
  • 31
  • 47

4 Answers4

1

This is the "casting operator". It has its own special syntax in the Java language (i.e. it is not a method call).

It works with Objects, too:

 BigInteger n = (BigInteger) session.getAttribute("a number");

It is used for two things:

  • Conversions between the different number types
  • Telling the compiler that a given object has a more specific type (*) than the compiler can infer. This will be checked at runtime, and if it fails, you will get a ClassCastException. The compiler will also check if the cast is completely impossible, and reject it at compile-time if that is the case.

(*) or even a less specific type

Thilo
  • 257,207
  • 101
  • 511
  • 656
  • 1
    Note that you can also upcast, i.e. make the declared type "less specific". This is, however, used more rarely. – Erik Hofer Nov 30 '16 at 08:09
  • @zero_cool: That's true. That can be used to influence method dispatch based on declared parameter types, for example. – Thilo Nov 30 '16 at 08:55
  • When you used the words "casting operator" it clicked in my brain. Thank you ALL for spending the time and effort to help this beginner. I am not sure if I should comment on each answer. – Guy Laclair Dec 01 '16 at 00:41
0

It is simple casting double to int. This is the syntax supported by java. You will come across similar kind of syntax when objects are converted to a particular class like this:

(ClassName) objectVariable...

You may use wrapper classes though such Integer, Float and can user their methods to perform casting as well.

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
0

Java allows casting and that is what (int)(5.4) does! so when you cast you tell the compiler that you need a type conversion those conversions must not necessarily be related to primitives, you can basically use any other valid DataType

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

What is Casting in Java?

Well, all casting really means is taking an Object of one particular type and “turning it into” another Object type. This process is called casting a variable.

What are the Rules behind Casting Variables? If you are going to cast a variable, you’re most likely doing what’s known as a downcast. This means that you’re taking the Object and casting it into a more “specific” type of Object. Here’s an example:

Object aSentenceObject = "This is a regular sentence";
String aSentenceString = (String)aSentenceObject;

You see what we’ve done here? Since the type Object is a very broad type for a variable, we are “downcasting” the variable to be a String type. Now, the question is, is this legal? Well, you can see that the value stored in the aSentenceObject is just a plain old String, so this cast is perfectly legal. Let’s look at the reverse scenario, an “upcast”:

String aSentenceString = "This is just another regular sentence";
Object aSentenceObject = (Object)aSentenceString;

Here we are taking a variable with a more specific type (String) and casting it to a variable type that’s more generic (Object). This is also legal, and really, it is always legal to upcast.


Widening Primitive Conversion

19 specific conversions on primitive types are called the widening primitive conversions:

  • byte to short, int, long, float, or double

  • short to int, long, float, or double

  • char to int, long, float, or double

  • int to long, float, or double

  • long to float or double

  • float to double

Narrowing Primitive Conversion

22 specific conversions on primitive types are called the narrowing primitive conversions:

  • short to byte or char

  • char to byte or short

  • int to byte, short, or char

  • long to byte, short, char, or int

  • float to byte, short, char, int, or long

  • double to byte, short, char, int, long, or float

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range.

Gal Dreiman
  • 3,969
  • 2
  • 21
  • 40