I was wondering if there is an easy way to declare a variable in android programming. I am still teaching myself however I haven't been able to find the answer to this question. I would like to do something like:
var1 = 'variable1';
var2 = 'variable2';
So that it can be called later in the code by its Variable.
Thank you in advance.
I suggest learning Java fundamentals first. Declaring a variable is fundamental to Java programming language (and indeed many programming languages), its not just Android specific.
You can learn Java fundamentals here.
Good Luck in your learning
What about
private static final String VAR = "string constant variable";
String var1 = "string instance variable1";
Android is basically Java so simple look for java references.
http://library.thinkquest.org/17862/jvar.htm
int a=10;
String b="kick";
char c;
Related
I had seen forums and questions that can be used kotlin in java but, with respect to my question is that I want to use the apache math commons library ("which is only available in java") within kotlin. My project is in intellij idea and I have imported the library correctly, I show you how it is written in java
import org.apache.commons.math3.distribution
NormalDistribution normalDistribution = new NormalDistribution(10, 3);
double randomValue = normalDistribution.sample();
```
A class is a class, regardless of if it's defined in Java or Kotlin. For the most part, this means you just do the Kotlin thing in Kotlin and the Java thing in Java, regardless of where the class you're using is defined. There are exceptions, like for static methods, but most stuff "just works".
I expect, knowing nothing about the NormalDistribution class, that this will work:
val normalDistribution = NormalDistribution(10.0, 3.0);
val randomValue = normalDistribution.sample();
Ok, so I was wrong initially. I had to change my literals above from (10, 3) to (10.0, 3.0). Here's a difference between Java and Kotlin. Kotlin doesn't do automatic numeric type promotion. So while I could use Integer literals for the equivalent Java code, in Kotlin, I had to use Double literals. But my IDE showed me this right away, including a tooltip message that told me just what was wrong. And this is a Kotlin thing, not a Java thing. The same thing would happen if I tried to call a method defined in Kotlin taking doubles as parameters, and I tried to pass it integers. This had nothing to do with which language NormalDistribution is defined in. After that exercise, I can say for sure that this Kotlin code works fine.
Maybe the issue is more that you just don't know Kotlin very well yet. Part of learning Kotlin is realizing how much of a non-issue it is to use Java classes in Kotlin code.
I have a next code in C#:
public CryptoTimeInForce? TimeInForce ;
public void metod(SomeClass baseTrade){
this.TimeInForce = new CryptoTimeInForce?(baseTrade.TimeInForce);
}
CryptoTimeInForce is Enum
public enum CryptoTimeInForce : byte
{
///values
}
How I can create enum in java to provide the same logic like in C# code?
Enum in java cant be instantiated and i cant repeat same code in java.
Is there any alternatives?
In java, you get the '?' part for free, since java enums are objects, so they are always nullable.
In java, you cannot have an enum derive from byte, but that should not matter, it is just a performance optimization.
There are many other differences between java enums and C# enums, but absolutely none that would be a problem for what you are trying to do, from the code you have shown us.
Also, as DavidG comment says, it is entirely pointless to be instantiating an enum in C#, so the fact that you cannot instantiate an enum in java should be irrelevant. Enums are not meant to be instantiated, they are just constants.
In Android/Java when I make constants and variables I generally do something like the following:
public/private static final int MY_CONSTANT = 73;
int myVariable = 37;
And in Swift I do
let myConstant: Int = 73
var myVariable: Int = 37
or just
let myConstant = 73
var myVariable = 37
Are the Swift versions equivalent to the Java versions under the hood?
I don't have a specific coding problem right now, but I am trying to gain a deeper understanding of the inner workings of both languages. The question came to mind when I was reading about Swift properties. Sometimes making a comparison or having a frame of reference helps me to do that.
I'm not sure if this question is appropriate for SO or not. I'll try asking and if it gets too many downvotes I'll delete it.
Using let in Swift is like specifying final in Java, while using var in Swift is like leaving out final in Java.
Swift also have Type Properties that are global to the type. The keyword for type properties is static, so it's exactly the same as in java. If you want a, in java terms, static final field then you write static let, and if you just want a static non-final field you write static var.
In JavaScript, you can do this.
String.prototype.removeNumericalCharacters = function(){
...code...
}
or
Number.prototype.addTwo = function(){
...code...
}
var a = 5;
a.addTwo();
//a is now 7
Is there a way to do something similar in Java? (I don't mean the actual function, just using that as an example)
An example in Java would be
int a = 5;
a.addTwo();
//A is now 7
My question is how do I define the .addTwo() method.
It's Friday so lets answer this question. I'm not going to dive into much details (it's Friday!) but hopefully you'll find this useful (to some extend).
You certainly know that Java objects don't have prototypes. If you want to add a field or a method to a Java class you have two options. You either extend the existing class and add the method/ field to it like this:
public class A {
}
public class B extends A {
int addTwo () {...};
}
However that's not changing the parent class. Objects of class A in the example still have no method addTwo.
Second approach is to dynamically change the class (you could use things like javassist) and method/fields to it. It's all fine but to use these new methids/fields you'd have to use reflection. Java is strongly typed and needs to know about class's available methods and fields during the compile time.
Finally and that's when things get really rough - primitive types, in your instance int, are 'hardwired' into JVM and can't be changed. So your example
int a = 5;
a.addTwo();
is impossible in Java. You'd have more luck with dynamic languages on JVM (Groovy is one of them). They're usually support optional typing and allow dynamic method calls.
So enjoy Friday!
This question already has answers here:
Does Java have "properties" that work the same way properties work in C#?
(5 answers)
Closed 9 years ago.
I understand that this question is similar to others asking about Java Properties, and the answers in those questions seem to be "no, you have to use getters/setters".
I searched for C#-like Java properties, but have only found one "hit" so far. And I'm not sure if it is a property or something else.
Reading the Java Tutorial, I came across some text and code that says (** emphasis and comment mine):
Finally, you can use the built-in **length property** to determine the
size of any array. The code:
//notice lack of brackets after anArray.length :
System.out.println(anArray.length);
will print the array's size to standard output.
Since Array has a length property, Java does have properties, right?
Can someone point to some documentation about them?
It really depends what definition you're using. Accessing some class's data without using parentheses is a very weak definition of "property." You can make any class/instance member field public and thus have it accessible without using a method.
That doesn't make it like a C# property where you can add custom logic to how getting and setting is done on that field though! You're straight up reading from/writing to a variable. The length field of an array is not writable and is in fact immutable, so it's not horrible style for that to have been done. But other classes that expose their fields have gotten into trouble, like Point and Dimension.
No. There's no documentation about them. Not real properties as in C#, Groovy, etc.
The JavaBean specification is as close as it gets, and that has more to do with satisfying the needs of tooling; it's still just getters, setters, and naming conventions.
"Length" is somewhat anomalous, along with other things about how arrays were implemented.
Java objects can have attributes, which you can think of as a type of property, and they also can have methods. However, "property" in the realm of C# means something specific, which Java does not have.
Of course, Java can mimic them with other features.
In Java, public member variables can be considered properties but they are just simple attributes; there is no way to provide getter/setter methods to intercept their reading/writing. For example:
class Foo {
public String name;
}
Foo f = new Foo();
f.name = "Alice";
f.name; // "Alice"
You can also make them "read-only", as long as you provide a constructor with assignment (or a default value):
class Bar {
public final String name;
public Bar(String name) { this.name = name; }
}
Bar b = new Bar("Bob");
b.name; // "Bob"
b.name = "Carol"; // Compile Error: cannot assign to final field.
No.
Java uses the JavaBeans specification (roughly: getters and setters) to accomplish this.
Java primitive objects have a handful of things you might regard as "properties", since they are specified as variableName.someName rather than variableName.someName(). length (for an array) is one (and the only one I can think of at present). However, in a bit of sleight-of-hand they are referred to as "fields" in the spec, even though they are not implemented at all like fields.
They are all read-only.