Using the Object class in Java - object

I understand that declaring variables of type Object or passing an Object variable as a parameter in a method is so that we can pass any object type e.g. Integer or String or an array. I just wanted to ask if we can also pass primitive data types or cast to integer primitive types too?
For example if I have a class Stack which allows us to push and pop objects of type Object, then i can use this class for Integer objects BUT can i use it for a primitive type int?

Yes you can, because Java will "auto-box" your primitive type. In other words, if you pass an int to your method, it will first get converted to an Integer, then that Integer will be passed as an argument to your method.
This tutorial gives more details about how it works.

Related

Get ocl string attribute length

I need to check an string attribute length and I don't know what function I have to use, size() maybe?
Context Myclass inv:self.string_attribute->size() <7
-> is for navigating from Collections.
. is for objects/values and an attribute is a value so:
...string_attribute.size()

How to specify any/unknown/variant type in GDScript?

Is there a way to specify an unknown type in GDScript?
GDScript docs use the type Variant for this (for example in Array.count method).
Say, I'd like to write an identity function. I can do it like so:
func identity(x):
return x
But I'd like to declare that both the parameter type and return value could be anything. Something like:
func identity(x: Variant) -> Variant:
return x
This doesn't work though. Variant is not a known type name. I tried various names, bot nothing seems to work.
Is the only option to leave off the type?
Yes, the only option is to not specify the type.
This function:
func identity(x):
return x
Takes Variant and returns Variant.
There is a Variant class defined in Godot in C++. Which, as you have found out, we cannot use it by name in GDScript.
Please notice that the docs use a notation based on C++. For instance int count (Variant value) does not only differ from GDScript because of Variant, but also in that you specify the type after the parameters not before the name, also you use func.
This is how int count (Variant value) looks in GDScript: func count(value) -> int:, and this is the C++ definition: int Array::count(const Variant &p_value) const (source). Compare the C++ definition with the one on the documentation.
For another example, this is how Array duplicate (bool deep=false) looks like in GDSCript: func duplicate(deep:bool=false) -> Array:, and this is the C++ definition: Array Array::duplicate(bool p_deep) const. (source). Notice the C++ definition does not indicate the parameter will be optional, that information is added for the script binding.

swift println float using string

I wish to ask a conceptual question. My code is to print an array of float values of 5 decimal places onto the console. Why must it be String instead of Float? Ans[y] is an array of type float.
println(String(format: "%.5f", Ans[y]))
Instead of Float
println(Float(format: "%.5f", Ans[y]))
Float gives an error of extra argument 'format' in call
You can use map() to format your Float array as string array. Btw you should give it a name starting with a lowercase letter. Try doing as follow:
let floatArray:[Float] = [1.23456,3.21098,2.78901]
let formattedArray = floatArray.map{String(format: "%.5f", $0)}
println(formattedArray) // "[1.23456, 3.21098, 2.78901]"
It's just a matter of understanding what your words mean. String is an object type (a struct). Float is an object type (a struct). The syntax Thing(...) calls a Thing initializer - it creates a new object of type Thing and calls an initializer method init(...). That's what you're doing when you say String(...) and Float(...).
Well, there is a String init(format:) initializer (it actually comes from Foundation NSString, to which String is bridged), but there is no Float init(format:) initializer - the Float struct doesn't declare any such thing. So in the second code you're calling a non-existent method.
You can use NSLog instead of println. NSLog is still in the foundation class and has the flexibility of specifying the exact format you need.

widening conversion not implicit when used with 'object' type?

The C# MSDN documentation states that widening conversions are implicit and dont require an explicit cast. Accordingly, I find that the following code works without giving any errors:
public void MyMethod(int x)
{
float f = x; //widening conversion, works implicitly as expected.
...
}
But, the following does not seem to work, even though this also appears to me to fall under the category of a widening conversion.
public static void MyMethod(int x)
{
object o = x; // implicit conversion - works.
float f = (float)o; // implicit conversion expected here also - but doesnt work...
}
In the above second piece of code, I would expect an implicit conversion to happen from the int data stored in 'o' to the type specified in the explicit cast(float). But this doesnt happen and this code throws an InvalidCastException. Why is this so? I can understand an exception being thrown when 'o' is assigned to 'f' without any cast. But if a cast is specified explicitly and converting to that cast requires an implicit conversion (i.e. int to float) which is supported by the language, why is an exception thrown ?
Thanks.
casts do different things at different times. This line:
float f = (float)o;
Is not attempting to change the type of o - it's attempting to unbox a float. Unfortunately, you can only (within a few wiggles1) unbox the same type of value that was boxed - a boxed int has to be unboxed as an int.
You would instead have to do:
float f = (int)o;
Where the (int) is performing the unbox, and then the implicit conversion can occur from int to float, as per your first example.
For more, read Boxing and Unboxing:
Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit...
1 There are some rules about Enums and their underlying type which I can't remember and won't ever deliberately use.

Type as a String

How do I convert a type to a string?
I thought something like this should work
import std.stdio: writeln;
import std.conv: to;
writeln(to!string(int));
Update: I found it at http://dlang.org/phobos/std_traits.html#.fullyQualifiedName
I guess all logic in D operating on types are given as templates arguments right?
int is already a type. You don't need to typeof it.
You could use the .stringof property to get a string representation. http://ideone.com/T4yYmo
writeln(int.stringof);

Resources