I have java class which has a variable of type "char" now I have to conver that class on AS3 side . does any one know whats the java char equivalent on actionsctipt (keeping in mind char accepty only one character )
This may not be what you want, but I would still use String. I have had this same situation in the past, and I found that using String still provided everything I needed. You could create your own special object with a member String and then control access through getters and setters to make sure that only a single char is assigned.
Technically you could use int (and I have in the past) to store a single char or bit array. There are no 'true' primitives that you want to use for a char however.
Related
I know there are various inbuilt functions, using which we can count string length like lastIndexOf(), by converting a string into a char array and counting iteration. There is a way in C programming for(i = 0; s[i] != '\0'; ++i); .
how to count string length in java like in C mentioned above.
Update:
Yes, there are these methods String.length(), String.toCharArray(). But if there are any other hard code way to find the string length.
Java is an object-oriented language, where instances are encapsulated, and you access instance properties by calling methods (or directly accessing fields, if they are exposed / not private).
In Java, String is a built-in class with instances and methods, other than in C, where a string is something like an array of characters without any encapsulation.
So, whatever you do with a String, you'll end up calling built-in methods of that class, e.g.:
toCharArray() is a built-in method.
charAt() is a built-in method.
So, use the straightforward String.length() method.
when I look at the java documents to see how to use methods.
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString()
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toString()
I find two methods that confuse me, first one is
static String valueOf(char[] data)
Returns the string representation of the char array argument.
that's the information I find on java docs, but I cannot find any information on how to use it, and how do I know I need to use String.valueOf() instead of something.valueof() based on java docs?
second method is
String toString()
This object (which is already a string!) is itself returned.
How do I know when it is to use Integer.toString() or something.toString() based on the information provided by java docs.
Could anyone tell me how to extract those information? This issue has bothered me for a long time. Happy holidays^_^
You need to have a method in a class before you can call it whether by using an instance of that class or using the class itself if it is static.
In the case here, valueOf() is only found in the String class and it is static so you can only call it using String.valueOf().
But that is not the case of toString() because it is not static and can be called through an instance like something.toString().
Basically they seem to perform the same function but valueOf() is treated special a bit. Such that it can take null values but toString() will throw a NullPointerException if variable of null value is used.
I cannot find any information on how to use String valueOf(char[] data), and how do I know I need to use "String.valueOf()" instead of something.valueof() based on java docs?
It's very straightforward. If you have a char array, and you want a String, you use this method. If you have something other than a char array, or you want something other than a String, you use a different method.
How do I know when it is to use Integer.toString() or something.toString() based on the information provided by java docs?
If you have an int or an Integer, and you want a String, then you use this method. If you have something that is not an int or an Integer, or you want something other than a String, you use a different method.
For instance, Double.toString() converts a double or a Double to a String.
Generally, you look at the class you want to convert to, and find a method that does the conversion. Sometimes, like the toString() method, the method is in the class you want to convert from.
Well, as title says it is a weird question. I want to create an int called int and of course if i write;
int int;
java(eclipse) gives error. So I've been wondering if it is possible to create such a thing? if you are asking me why i insist on this, I've created str for strength, agi for agility, but int for intelligence can not be created. So should I just describe it in another way or is there a simple way to do it?
The identifier int is a reserved keyword in Java and cannot be used as a variable name.
I would suggest not abbreviating your attributes, but instead use full words:
int strength;
int agility;
int intelligence;
Even if there were a way to create a variable named int, the resulting code would be confusing to read later.
You can't. int isn't a valid identifier in Java, because it's a keyword.
From JLS 3.8:
An identifier cannot have the same spelling (Unicode character sequence) as a keyword (§3.9), boolean literal (§3.10.3), or the null literal (§3.10.7), or a compile-time error occurs.
Compare this with str, which isn't a keyword. (And neither is String, in fact.)
As Greg says, it's clearer to use full names (strength, intelligence etc) anyway.
No, you can't, because it is keyword, i.e. reserved word in Java. Call it smth like _int or int_
It is a reserved word. You can not use it as an identifier.
You can't do that. It's a reserved word.
I'm fairly new to Java and I'm trying to use the + character as part of an enum type, but the compiler is complaining about the syntax because I believe it sees it as an operator.
I'd like to do the following:
enum mediaType{
FACEBOOK,GOOGLE+,TWITTER;
}
Any ideas?
Thanks!
Yes, the compiler will treat + as an operator. You can however choose a different name there:
enum mediaType{
FACEBOOK,
GOOGLE_PLUS,
TWITTER;
}
And if you want to use the value GOOGLE+ only, then have a field of type String, storing the value, and also a parameterized constructor.
P.S: As per proper naming convention, the enum name should be MediaType.
You can't use arithmetic symbols in identifiers. You need to find something you can use like GOOGLE_PLUS
Maybe reading the official Java tutorial's section on Naming Conventions will help you: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
You could at least define a private String myName = "Google+"; inside the enum, and define a method that a UI can use to get the value you want rather than just displaying the enum's variable itself.
public String myName() {
return myName;
}
Many people believe that you are limited to ASCII in Java just like C and C++.
Actually you have the full Unicode character set.
This is perfectly good Java:
enum Plus {
Google,
GooglePlus,
Googleᚋ,
Googleᐩ;
};
Not quite a + (which you cannot have in an enum because it will be confused with the + operator) but it will still carry the impression of a plus.
It seems you can use the Ogham character called muin which looks a bit like a plus character. Alternatively it seems the Canadian syllabics final plus is also acceptable alhough a number of the other possibilities seem not to be acceptable.
I'm developing a java application that uses some jni calls.
I have on C code the following variable:
GLuint *vboIds;
I want to pass this variable from Java to C, but I don't know how to declare it in Java.
GLuint is equivalent an unsigned int.
So, I think this is the equivalent declaration in Java:
int[] vboIds;
What do you think?
Thanks
You don't say explicitly whether it is meant to be a pointer to a single value or an array, but I'd guess it's probably an array from the naming and what you are thinking of doing with the mapping (there should also be a parameter somewhere that specifies the length of the array; those both map to the same argument on the Java side as Java's arrays know their own lengths). You're probably right to use an int as that's generally the same size as a C int – not that that's a guarantee, not at all, but hardly any machine architectures are different from that these days – but you'll need to watch out for the fact that Java's numeric types are all signed. That's mostly not a problem provided you're a bit careful with arithmetic (other than addition, subtraction and left-shift, which work obviously) and comparisons.