How to create an int named "int" in Java - java

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.

Related

Is length member from the String class an integer constant?

I am trying to understand if length member from the String class an integer constant? I'm thinking it is not.
It is NOT a compile time constant expression in the sense of JLS 15.28.
The expression str.length() is a method call, and no explicit method calls are constant expressions. This is true even if the String is a literal; i.e. "hello".length() is NOT a compile time constant expression according to the JLS.
It is a runtime constant, in the sense that once the String has been created, the length cannot change. This follows from the fact that Java strings are immutable.
You can confirm this by looking at the source code of any version of the (standard) String class. The code is liable to differ between versions, but the "runtime constancy" property will remain the same across all versions.
(Actually, the above is not completely accurate. If you are willing to use bad reflection to break encapsulation, you can modify the private members of a String and mutate it. That can change the value returned by length(). You could achieve the same effect via native code methods, and possibly other means. But ... just ... don't do it! The JLS says that if you do that kind of thing, the behavior of your Java code is unspecified.)
According to the source code (jdk9) for the String class, length():
public int length() {
return value.length >> coder();
}
Where value is
#Stable
private final byte[] value;
And coder() is:
byte coder() {
return COMPACT_STRINGS ? coder : UTF16;
}
So it is essentially a runtime constant as String's are immutable and the length of value will be decided at compile time.
length member from the String class is a method, returning an int value, representing the number of characters of the String.

Using + in a java enum type

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.

Naming convention for member variables with initials

I was wondering how someone would name the member field that began with initials as opposed to words.
public class MyClass {
// I know this is how it is for a member field with words.
private String myString;
// What about this String? It is representing the term "RV Scale" which is
// short for "Reclose Volts Scale."
private String RVScale;
}
I know that I could just use private String recloseVoltsScale, but I'd rather keep it RVScale. If anyone knows if its supposed to be rVScale or RVScale or something else I'd appreciate the info.
EDIT :
Also, what about a member field like this...
private int RV;
as per java naming convention member field should start with mixedcase so it should be rvScale and member field like RV should be like private int rv;
Using the right letter case is the key to following a naming convention:
Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage).
Uppercase is where all the letters in a word are written in capitals. When there are more than two words in the name use underscores to separate them (e.g., MAX_HOURS, FIRST_DAY_OF_WEEK).
CamelCase (also known as Upper CamelCase) is where each new word begins with a capital letter (e.g., CamelCase, CustomerAccount, PlayingCard).
Mixed case (also known as Lower CamelCase) is the same as CamelCase except the first letter of the name is in lowercase (e.g., hasChildren, customerFirstName, customerLastName).
You should spell it out.
That being said, I would keep it rvScale.
Keep in mind that the only one that should NOT be used is rVScale since this can cause some confusion with introspection.
This case would generate getter and setter named getRVScale and setRVScale, but using bean introspection to find the property associated with these, would search for a property named RVScale, which doesn't exist. This is due to the fact that when searching for the corresponding property, the first letter after the get/set is lower cased, unless there are two adjacent letters in upper case (RV), in that case, the name is left as is, producing RVScale.
Like explained here:
Javabean convention - method naming for property gId
Do not use shortcuts unless you need to. Even if they are private (or maybe because they are), name variables clear enough. If potential code reader does not understand rv as shortcut of something like Reclose Volts, do not use shortcut. If you need provide hint it is shortcut actually, you are hinting he should lookup it somewhere what does that mean. Where will he find it? If it is in comment, you have wrong name of member, as you have to explain what does the name mean. Unless you application is all about Reclose Volts. I suggest to follow java conventions as they did have reason to appear.

What are the valid characters for a Java method name?

I read about the naming of Java variables. It says that Java variables cannot start with any numbers and special characters except for $ and _.
Some valid examples:
int count;
int _count;
int $count;
And some invalid examples:
int %count;
int 4count;
int #count;
Do the same rules apply to method names?
Yes, method names and variable names are what's called "identifiers". Identifiers all share the same rules regarding accepted characters. Take a look at §3.8 from the Java Language Specification to find out exactly what an identifier may contain, and §6.2 for an explanation about how identifiers are used.
You might be surprised when having unusual characters for method, such as:
public void mój_brzuch_zacznie_burczeć()
and it works pretty nice. Take a look at this blog to see more fancy examples.
From the Java Tutorial:
"Although a method name can be any legal identifier, code conventions restrict method names."
http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Java Char on actionscript

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.

Categories

Resources