I'm learning JAVA..

ilan

Expert
Licensed User
Longtime User
hi,

i started to learn JAVA, for now i use Youtube tutorials for that, you have really enough and also udemy got some free JAVA courses.

now i see how much work @Erel had put on b4x. Java is so different then VB

at this moment i am really curious to know how erel convert this simple JAVA code to B4A.

in JAVA you have "=" and "=="
if you want to set a value you use "=" if you want to check if something is equal you use "=="

now in b4a i always use "=" but how does B4A know when i ask for equal or when i want to set a value to an integer for example, like:

JAVA:

B4X:
int age;
age = 5;

if (age == 5 || age <5) {
    age = 6;
} else {
     System.out.print("You are to old");
}

B4A:

B4X:
Dim age as int = 5

if age <= 5 then age =6 else log("You are to old")

you can see that one time i use "=" to ask if is equal and one time i use "=" to set the new value to "age"
but if b4a converts the code to JAVA how does he know when to convert "=" to "=" or to "=="

just curious :)
 

ilan

Expert
Licensed User
Longtime User
btw, in VB i can put more then 1 statement after Then with ":" like:

B4X:
dim age as int = 5

if age <= 5 then age = 6 : log("you are now one year older") else log("you are to old")

can i do this also in b4x?
 

wonder

Expert
Licensed User
Longtime User
Something like this? ;)
B4X:
Sub MainCycle
    ...
    InputText.Replace("=", EqualSelection(InputText))
    ...
End Sub

Sub EqualSelection(InputText As String) As String
    If ConditionalOperator(InputText) Then
        Return "=="
    Else
        Return "="
    End If
End Sub

Sub ConditionalOperator(InputText As String) As Boolean
    If InputText.ToLowerCase.Contains("if") Or _
       InputText.ToLowerCase.Contains("or") Or _
       InputText.ToLowerCase.Contains("and") Or _
       InputText.ToLowerCase.Contains("then") Or _
       InputText.ToLowerCase.Contains("case") Or _
       ... _
    Then
        Return True
    Else
        Return False
    End If
End Sub
 

ilan

Expert
Licensed User
Longtime User
Something like this? ;)
B4X:
Sub MainCycle
    ...
    InputText.Replace("=", EqualSelection(InputText))
    ...
End Sub

Sub EqualSelection(InputText As String) As String
    If ConditionalOperator(InputText) Then
        Return "=="
    Else
        Return "="
    End If
End Sub

Sub ConditionalOperator(InputText As String) As Boolean
    If InputText.ToLowerCase.Contains("if") Or _
       InputText.ToLowerCase.Contains("or") Or _
       InputText.ToLowerCase.Contains("and") Or _
       InputText.ToLowerCase.Contains("then") Or _
       InputText.ToLowerCase.Contains("case") Or _
       ... _
    Then
        Return True
    Else
        Return False
    End If
End Sub

ok, this make sense, so what you are saying is, if "=" is between "if" ... and "Then" then its always a "==" else is "=" .. (simple :))
 

sorex

Expert
Licensed User
Longtime User
actually the : is only needed if you want to group commands in 1 line.

you could also reduce

B4X:
if (age == 5 || age <5) {

to

B4X:
if (age <6) {

not?
 

ilan

Expert
Licensed User
Longtime User
actually the : is only needed if you want to group commands in 1 line.

you could also reduce

B4X:
if (age == 5 || age <5) {

to

B4X:
if (age <6) {

not?

Yes, but i only asked how erel know when to use = or ==

If i would use the code like you did, my question would be useless
 

sorex

Expert
Licensed User
Longtime User
in JAVA you have "=" and "=="

not sure about jave but some also have === !== for specific checks where the others might return null.

all to make programming "easier" ;)
 

wonder

Expert
Licensed User
Longtime User
ok, this make sense, so what you are saying is, if "=" is between "if" ... and "Then" then its always a "==" else is "=" .. (simple :))
I'm only guessing... :) I'm sure Erel has some other tricks up his sleeve.
 

sorex

Expert
Licensed User
Longtime User
oh I see, I didn't know you were talking in detail about b4a>java.

the pre-compiler knows based on the keywords used in the lines I guess.
if a line starts with if he sets a trigger for it indicating that variable checks are or might be coming.

if the first "word" is not a keyword it's usually a call or a variable being set.
 

ilan

Expert
Licensed User
Longtime User
Translating from B4X language to Java is not so difficult. The difficult parts are to create a complete and coherent framework, an IDE, debugger, designer and other components.

This is an awesome work you did and do...
If we compare the first version of b4a to the last one is like windows 3.1 and windows 10.

Only they needed 25 years for that and you did it in 7 :)


Btw, is it possible to add in an if statement ":" like in vb?

If age = 5 then age = 6 : log(age)

And also what could be a great update to the next version is to be able to test a code without to install on the phone. Like i can do in java or xcode. Sometimes i only want to test a code and get the result to the log so no need to connect phone to run it... would be great to have such a feature. Maybe it can be done with b4j?
 

ilan

Expert
Licensed User
Longtime User
Use a VM like Genymotion then

I am but why is it necessary to install a 10mb app to test only 1 sub??

Dont u think it would be great to be able to run only 1 sub and get the result to the log in less then a second???
 

sorex

Expert
Licensed User
Longtime User
if seems to be an issue with the else keyword checking.

this works fine
B4X:
Dim x=5
If x<=10 Then     Log(1):Log(2)
 

ilan

Expert
Licensed User
Longtime User
if seems to be an issue with the else keyword checking.

this works fine
B4X:
Dim x=5
If x<=10 Then     Log(1):Log(2)

you are right, so maybe its a bug? it should work also if an else is behind the second line...


VB 2010:
B4X:
        Dim age As Integer = 6
        If age < 6 Then age = 4 : My.Application.Log.WriteEntry(age) Else My.Application.Log.WriteEntry("5")
 
Last edited:

sorex

Expert
Licensed User
Longtime User
this code gives the errors attached to the lines.

B4X:
If x<=10 Then     Log(1):Log(2)
Else Log(3):Log(4)   <- Syntax error. Statement should start in a new line (well, it is :) )
End If <- missing keyword: end sub
end sub <- syntax error
 

sorex

Expert
Licensed User
Longtime User
it seems that from the moment when ELSE is used it forces you to use multiline.

this works then

B4X:
If x<=10 Then
Log(1):Log(2)
Else
Log(3):Log(4)
End If
End Sub
 
Top