Saturday, November 16, 2024

Common String methods in Java

Almost all programming languages use Strings I'm sure. In Java, they are used a lot, and the language even made Strings it's own class. So, unlike other data types like `int` or `boolean`, Java's String data type is actually an object which is why we capitalize it when initiating a string.

Since String is an object, it also comes with it's own String methods. A full list of String methods can be found at this W3Schools link: https://www.w3schools.com/java/java_ref_string.asp

Below are some common ones:

  • length(): returns the length of a String as an int
  • concat(arg): returns a combination of String1 and the arg of the method - which could be another String or a mixture of blank spaces and a String.
    • String1.concat(" " + String2); as an example of multiple values in the argument
  • indexOf(arg): returns the index of the argument given
  • charAt(arg): returns a character at index arg
  • equals(arg): you cannot use the == operator to compare two strings in Java, so you need to use this method instead. This is case sensitive, but you can also use equalsIgnoreCase() as an alternative if you don't care about the case (or don't want to for the sake of your program's objective).
  • substring(arg, [arg2]): this returns a String that falls in line with the argument(s) you give the method signature. If you give just one argument, it will return a String that starts at that index all the way to the end of the String. If you declare two arguments, it will return a String that starts at the index of the first argument and will go up to but not including argument two.
  • toUpperCase()/toLowerCase(): these return a string in all caps or all lowercase. This is good for situations where you want to make sure case is ignored and you just want to compare the characters in the String.

Friday, November 15, 2024

Installing Java on my laptop computer

Last year I purchased a new Macbook which uses the M2 chip. I haven't come across any questions when installing apps, since most sites just give a MacOS option. But currently, Apple has active support for two types of MacOS computers (laptop, desktop, minis) - the Intel computers which are older and being phased out, and the newer computers with M-series processors. For some sites that are more technical or detailed, this has changed the way they display install instructions for their software, because the file types could be different between the two types of MacOS options.

I bet most sites determine your computer's processor and just display the right option automatically. But the Java site currently does not. It gives you two options for MacOS Java downloads (well actually they give you four). Below is a screenshot of what you will see if you download it from Oracle's site.


Looking at it, there are two main types of install files here: ARM64 and x64. What does this mean?

If you have an Intel MacOS computer, you'll want to install the x64. This is the traditional way to refer to the Intel processors (if you're old enough, you'll remember x86 being the 32-bit version). x64 is the 64-bit version of an install file, which probably won't matter these days since most computers out there do not use the 32-bit operating systems. AMD64 is another option for this type of architecture.

If you have a MacOS M-series computer though, you'll want to get the ARM64 file. Some You sometimes may see "aarch64", and this is the same thing as ARM64. This type of file is what you will want if you download to a computer with Apple silicon.

How can you tell which you have if you are using a Macbook? Well find the "About Your Mac" menu and take a look at the processor if comes with. If it says Chip  Apple M1 Pro (or M2, M3, soon to be M4...), that means you will want the ARM64/aarch64 file. 

Otherwise it would give you an Intel chip type by saying something along the lines of Intel Core i5 (or i3, i7, and i9). These are various Intel chip levels, and you'd want the x64 file type.