innovativevast.blogg.se

Downcast in java
Downcast in java










downcast in java

So, unless you're doing something with error handling, then your program will likely exit or you'll get an ugly error message on your webpage.

#Downcast in java code

This is what's known as a run-time exception, as it's really only detectable when your code is running. If you were to try to cast something like a Date object to an Integer object, then you'll get a ClassCastException. Well, there is a certain amount of risk that goes along with downcasting your variables. This is quite a valuable approach to storing Objects in a Map, because if we had created the Map with something more specific than Object as the value, then we would be constrained to only storing Objects of that specific type (and its sub-classes). This would then give us more flexibility with those three variables, because now we have an actual birthday Date object, so we have access to methods like getTime() instead of just the default Object methods.

downcast in java

Since we know that the “name” is a String, and the “email” is a String, and the “birthday” is a Date, we can do these three downcasts safely. You see how we did three separate casts there? Since all the objects stored in the map are of type Object, this means that they are very generic and could most likely be downcasted.

downcast in java

String email = (String)model.get("email") ĭate birthday = (Date)model.get("birthday") This Map is a candidate for casting, and here's how we would deal with the casting: String name = (String)model.get("name") Here's what that Map would look like in code: Map model = new HashMap() So, an example of what would appear in this model would be: The Map stores key/value pairs, where the key is a String and the value is usually just the generic Object type. This “model” represents data that I want to display on a webpage, and the model is essentially a generic Map. For example, in my line of work, I deal with web applications, and I'm always dealing with something called a “model”. There are times when you want to get more specific functionality out of a generic Object. What are the benefits of casting variables? This is also legal, and really, it is always legal to upcast. Here we are taking a variable with a more specific type ( String) and casting it to a variable type that's more generic ( Object). Object aSentenceObject = (Object)aSentenceString Ĭopy to Clipboard String aSentenceString = "This is just another regular sentence" Let's look at the reverse scenario, an “upcast”: String aSentenceString = "This is just another regular sentence" 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. 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. String aSentenceString = (String)aSentenceObject Here's an example: Object aSentenceObject = "This is just a regular sentence" This means that you're taking the Object and casting it into a more “specific” type of Object. If you are going to cast a variable, you're most likely doing what's known as a downcast. It's fairly simple, you remember our talk about how everything in Java is an Object, and any Object you create extends from Object? This was inheritance, and this is important to understand when dealing with casting. What are the Rules behind Casting Variables? But, as with other languages, in Java you cannot cast any variable to any random type.

downcast in java

This topic is not specific to Java, as many other programming languages support casting of their variable types. This process is called type casting a variable. Type casting means taking an Object of one particular type and “turning it into” another Object type. If you want to be a programmer in any language, then type casting variables is something that you should definitely be very familiar with.












Downcast in java