Java code snippets

Nah, I always do that

import *;

Discussion

What does override mean?

Discussion

LinearLayout Password = (LinearLayout) findViewById(R.id.mainLinearLayoutPassword);
EditText passwordPass = (EditText) findViewById(R.id.PasswordPass);
String thePass = "1234";
String enteredPass = passwordPass.getText().toString();
if (enteredPass == thePass){
Password.setVisibility(0010);
}

Discussion

Why in java , when we use String , we do not consider of using new operator ?
String is a class , so any Object of this class should be instantiated by

 new 

keyword , so why there is no must in using new for String objects ?
eg:
String str = new  String();

and
what is the difference between
String str = new String();

and
String str = " string " ;

Discussion

Language:

Java


Source:
class Rextester { 
public static void main(String[] args) {
for(int i = 0; i < 10; i++)
System.out.println((int)Math.random() * 400);
}
}


Result:
0
0
0
0
0
0
0
0
0
0

Discussion

Language:

java


Source:
class Rextester{   
public static void main(String[] args) {
String ip = "255.249.249.256";
System.out.println(ip.matches("((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(.|$)){4}"));
}
}


Result:
true

Discussion

 "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(.|$)){4}"

Discussion

 rextester_bot java //code

Discussion

Language:

java


Source:
public class Program
{
public static void main(String[] args) {
System.out.println("hello");
}
}


Errors:
source_file.java:1: error: class Program is public, should be declared in a file named Program.java
public class Program
^
1 error

Discussion

Language:

java


Source:
class Rextester
{
public static void main(String[] args) {
System.out.println("hello");
}
}


Result:
hello

Discussion

Errors:

Method 'main' must be in a class 'Rextester'.


Tip:
Mistake? Edit your message, I'll adjust my response.

Discussion

The t in Rextester shouldn't be capitalized

Discussion

Java:
Thing thing = new Thing();
Kotlin:
thing = new Thing();

Kotlin knows the thing variable is of type Thing because of the initializer, so you don't have to specify like in Java

Discussion

variable.method();

Discussion

A static method?
Class.method();

Discussion

Does any one know why it throws me a NullPointerException at the second 'if'

 FXML
private void handleButtonAction(ActionEvent event) {

List<Integer> numeros;
List<Integer> numerosMostrar;

numeros = new ArrayList<>();

for (int i = 0; i <= 250; i++) {
numeros.add(i);
}

txtrResultados.setText(String.valueOf(numeros));

txtrResultados.clear();
if (event.getSource().equals(tgglbttnPares)) {
for (Integer numero : numeros) {
if (numero % 2 == 0) {
numerosMostrar.add(numero);
}
}
}
txtrResultados.setText(String.valueOf(numerosMostrar));
}

Discussion

'public' means it's available from the outside of the class

'static' means it's a class method, so you don't have to do:

MyClass myClass = new MyClass();
myClass.main();

But you can instead do:
MyClass.main()

'void' means it doesn't return anything

Discussion

      try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex) {
}

Discussion

Maybe i need to put more commands in the main?

public static void main(String[] args) {
gui g = new gui();
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception ex) {
}
}
}

Discussion

Energijs has been warned (1/3)

Discussion

Something like this ?

public class tab1 extends gui implements Runnable {


Override
public void run() {
jTextArea1.append("clicked\n");
}

public void start()
{
Thread t = new Thread();
t.start();
}

}

Discussion

And in the gui class

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
tab1 t = new tab1();
t.start();

}

Discussion

Gui class

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
tab1 t = new tab1();
}


thread class
public class tab1 extends gui implements Runnable {


Override
public void run() {
jTextArea1.append("clicked\n");
}

public static void main(){
(new Thread(new tab1())).start();
}
}

Discussion